#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;}
Comments