Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strings??

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I cant seem to figure out the algorithm (or function) that will allow me to COUNT the number of times a certain string appears in another...thanks in advance!
 

#include<string>
using namespace std;
int main()
{
string xxxxx = &quot; tr tr tr tr&quot;;
int count_of = 0;
int c = xxxxx.find_first_of(&quot;tr&quot;);
if( c != -1 0)
{
while(c != -1)
{
count_of++;//the count of tr
x = c;
//find next, when c is -1 means string doesn't exist
///anymore

c = string(xxxxx.begin() + x + 1).find_first_of(&quot;tr&quot;);
x += c;//the real position, if c != -1
}
}
return 0;
}
Ion Filipski
1c.bmp


filipski@excite.com
 
char my_string[] = &quot;abcabcabcabc&quot;;
char* ptr = my_string;

int occurance = 0;
while(ptr = strstr(ptr,&quot;abc&quot;))
{
ptr = ptr+1;
occurance++;
}

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top