Hello, i must find every substring in string and output position of it. But i have no idea how i can find another substring in string when i already find one.
Its should looks like this Input & Output
Enter a string, a substring, and a character:
writingstringfunctionsforfarstrings str s
String 'str' found, position(s): 7, 28
Character 's' found 4 times.
but my code doing only this:
Enter a string, a substring, and a character:
writingstringfunctionsforfarstrings str s
String 'str' found, position(s): 7
Character 's' found 4 times.
What you could do is have the find operation in a while loop, and each there's a successful match, start the next find operation from where the substring was found. The loop would terminate when find() finds nothing.
yes i know what i want do but i dont know how :( if i do something like this its stuck
int index = 0;
for(unsigned int i = 0; i < s1.size();i++)
{
if(index = sx.find(s2))
{
cout << "String '"<< s2 <<"' found, position(s): "<< index << '\n';
i = index;
break;
}
}
Use the overload of find() which also takes a position as a parameter. start with position 0, then keep finding as long as the return value of find isn't string::npos.
Use an array to keep track of the positions at which the substrings were found, and use another counter to keep track of how many times it was found, then print the values at the end.