I really want to start learning how to wield the string class and using it to make games and programs and stuff, but before i get too far, i created a small program that searches for a string, is it the most efficient way to write it?
Something like this? i'm a little confused, i'm trying to convert the for loop to the while loop but it keeps looping infinitly because i believe it has something to do with string never being a new position.
string stringToSearch = "This is a string of text to search.";
cout << stringToSearch << endl;
cout << "Enter a string to find: ";
unsignedint wordAppearance = 0;
int position = 0; // Edited
while( position < stringToSearch.size() )
position = stringToSearch.find( stringToFind, position );
if ( position == string::npos ) break;
cout << "Found instance number " << ++wordAppearance << " of " << stringToFind << " at position " << position << endl;
position++; // move forward or it finds the same thing again
if ( !wordAppearance ) cout << stringToFind << " not found ";