Mar 21, 2013 at 11:20am Mar 21, 2013 at 11:20am UTC
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".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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.
Last edited on Mar 21, 2013 at 11:55am Mar 21, 2013 at 11:55am UTC
Mar 21, 2013 at 11:56am Mar 21, 2013 at 11:56am UTC
http://www.cplusplus.com/reference/string/string/find/
And some more example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <string>
using namespace std;
int main (void )
{
int num=0; //number of "fi"s
string text; //input text
size_t retval; //return value from find()
int start_pos = 0; //starting seach position
getline(cin, text);
while (1)
{
retval = text.find("fi" , start_pos);
if ( retval == string::npos ) //no "fi" in string
break ;
//else
num++;
start_pos += retval+1;
if ( start_pos > text.length() ) break ;
}
cout << endl << num << endl;
return 0;
}
lol and wrapped:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <string>
using namespace std;
int countString(string*, string);
int main (void )
{
string text; //input text
getline(cin, text);
cout << "num of fi's : " << countString(&text, "fi" ) << endl;
cout << "num of ff's : " << countString(&text, "ff" ) << endl;
cout << "num of fl's : " << countString(&text, "fl" ) << endl;
return 0;
}
int countString(string *_input, string _find)
{
int num=0; //number of "fi"s
size_t retval; //return value from find()
unsigned start_pos = 0; //starting seach position
while (1)
{
retval = _input->find(_find, start_pos);
if ( retval == string::npos ) //no "fi" in string
break ;
//else
num++;
start_pos += retval+1;
if ( start_pos > _input->length() ) break ;
}
return num;
}
Last edited on Mar 21, 2013 at 12:19pm Mar 21, 2013 at 12:19pm UTC
Mar 21, 2013 at 9:19pm Mar 21, 2013 at 9:19pm UTC
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 =).