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.
My apologies, @Ch1156, I missed your updated thread.
Actually, the logic was more tortuous than I realised. The following works (I think) but there are probably better ways. I'm not fond of having multiple logic tests per loop.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string stringToSearch = "This is a string of text to search.";
cout << stringToSearch << endl;
string stringToFind{};
cout << "Enter a string to find: ";
getline(cin, stringToFind);
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 ";
}
> What happens if it DOESN'T find the string?
the program terminates without printing to the console.
¿is there any problem with that?
Oh I don't have problems with programs writing nothing.
I do have problems when I try running it in C++ shell and it cripples my web browser by printing out line after line of garbage.
for(unsignedint position = stringToSearch.find(stringToFind, 0); ...
I think that in the absence of finding the requisite string this ought to set position to string::npos ... and then proceed to enter the loop ...
... or perhaps not, if position is unsigned and string::npos comes back as -1
I think that in the absence of finding the requisite string this ought to set position to string::npos ... and then proceed to enter the loop ...
No, the for loop acts like a while so if the condition is not satisfied it would not enter the loop at all.
... or perhaps not, if position is unsigned and string::npos comes back as -1
OP's code simply doesn't work because he's using unsigned int instead of size_t.
Basically the loop will never end because pos is unsigned and will never be -1.
#include <iostream>
void to_search(const std::string& , const std::string& );
int main()
{
auto data = "mango juice,milk,sugar,lemon juice,potato,lettuce,orange juice,banana juice";
auto phrase = "juice";
to_search(data,phrase);
}
void to_search(const std::string& here, const std::string& stringToFind)
{
std::size_t position{},wordAppearance{} ;
while((position = here.find(stringToFind,position)) != std::string::npos)
{
std::cout << "Match " << ++wordAppearance << ": instance of " << stringToFind << " found at position " << position << '\n';
position += stringToFind.size();
}
if(wordAppearance == 0)
std::cout<<"the string \'"<<stringToFind<<"\' was not found\n";
}
Match 1: instance of juice found at position 6
Match 2: instance of juice found at position 29
Match 3: instance of juice found at position 57
Match 4: instance of juice found at position 70
Sorry for the late reply, thank you for your responses. I thought that size_t was the same thing as unsigned int but reading up on it, I found out that it wasnt.