Answer to Question #46262 in C for erico fire

Question #46262
write a program that will accept a string and character to search.the function will call a function ,which will search occurrence position of the character in the string and return its position .function should return - 1 if the character is not found in the string.
Expert's answer
#include <string>
#include <iostream>
int findChar (std::string& sentence, char key) {
for ( int i = 0; i < sentence.length(); i++ ) {
if ( sentence[i] == key ) {
return i;
return -1;
int main () {
std::string sentence = "This is a test sentence";
std::cout << "Here is our string " << "\"" << sentence << "\"" << std::endl;
std::cout << "The symbol 'e' can be found on the " << findChar(sentence, 'e') << " position in our string" << std::endl;
std::cout << "The symbol 'l' can not be found in the string, a result of what we have " << findChar(sentence, 'l') << std::endl;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog