Hello. I'm wondering what is another way or a better way to find 2 character sequences in a text? In this case, the code searches for "ff", "fi" and "fl".
#include<iostream>
using std::cin;
using std::cout; using std::endl;
int main()
{
unsigned ffCnt = 0, fiCnt = 0, flCnt = 0;
char text;
int tcsCount = 0; //Determine if two-character sequence.
while (cin >> text)
{
if (tcsCount == 1) //ENDS: Determine match of 2nd character.
{
if (text == 'f')
{
++ffCnt;
tcsCount = 0;
}
if (text == 'i')
{
++fiCnt;
tcsCount = 0;
}
if (text == 'l')
{
++flCnt;
tcsCount = 0;
}
else tcsCount = 0; //Avoid looking for false match. ie "foooi" == ++fiCnt.
}
if (text == 'f') //STARTS: Determine 1st char as 'f'.
{
++tcsCount;
}
}
cout << "Number of times 'ff' appears: " << ffCnt << endl;
cout << "Number of times 'fi' appears: " << fiCnt << endl;
cout << "Number of times 'fl' appears: " << flCnt << endl;
return 0;
}
Also is it possible to read a text as a whole? Or we can only go about it one character at a time? Like if we want to determine duplicate words. Any help would be awesome.
Thanks for the reply. Your code doesn't seem to give accurate results though, but I appreciate the link to string members and your demonstration on how it works, thank you =).