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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get all the substring in a long string?!?!

Status
Not open for further replies.

rhpingwoo

Programmer
Mar 5, 2003
15
AU
Hi,

I am trying to read a txt file. And make a count on how many times certain string appear in the file. But i am not sure how to break the whole line of the text into each individual string to compare it. Help.

Here is what i have for now:

// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main ()
{
char buffer[2000];
//char str="test";
int count;

ifstream inputfile ("testing.txt");
count=0;

if (! inputfile.is_open())
{ cout << "Error opening file"; exit (1); }

while (! inputfile.eof() )
{
inputfile.getline (buffer,2000);

if (buffer.contains("test"))
count=count+1;

cout << buffer << endl;
cout << "count = " << count << endl;
}


return 0;
}

I want to see how many time the string "test" appear in testing.txt

Raph
 
am i on the right track??

As i am trying to get each line of the file one by one, then break them up into seperate string, and then do the comparsion!!

Is there other better/faster way of doing this?
 
If you were looking for "the" in
"the cat sat on the mat"

Are you expecting to get
count = 1
or
count = 2

--
 
I can perfrom the task with the follwing code, because i know the file will only have the string i wanted to count appear once in each line.

Any suggestion on how i can do it if the string appear twice in 1 line??

as Salem mention above I will be expecting the count=2
If I am looking for "the" in
"the cat sat on the mat"


// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


int main ()
{
char buffer[2000];
char line[5000];
char str[]="test";
char file[]="testing.txt";
int count;

ifstream inputfile (file);
count=0;

if (! inputfile.is_open())
{ cout << "Error opening file"; exit (1); }

while (! inputfile.eof() )
{
inputfile.getline (buffer,2000);
strcpy(line,buffer);

if(strstr(line,str)!=NULL)
{
count=count+1;
}
}
cout << "count = " << count << endl;

return 0;
}

 
I have done a parser some time ago and I have done it like that:

1. see how long the looked for string is (let say n=3 for "the")
2. read the file line by line as you have done
3. make a pointer p to the string reprezenting thje curent line
3. read n caracters from the line from p position (best with strncpy)
4. compare with what you want to find
5. increase p (p++)
6. go to point 3 here and repeat until you have finished the line
7. repeat this with next line and so on

Of course you have to mix in some other variable to keep count of what you have find, but the logic is like that and is fast.

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top