How can i find number of "string"'s in array?
I have a file that i read into a buffer[20000]. How can i find the number of times the string "FILENAME" appears in the buffer? At the moment i am using strstr(buffer,"FILENAME") and then when it finds FILENAME i increment the pointer by 1 turning each char into 0 so that strstr doesnt keep finding the same instance of FILENAME:
FileIndex=strstr(buffer, "FILENAME")
for (int x=0;x<8;x++)
{
FileIndex[x]=' ';
}
NumFiles++;
I know this isnt a very good way of doing but it worked up until today where it is now finding FILENAME where it has supposed to have cleared it.
Can anyone suggest a better way of finding all instances of a string in an array?
any help is appreciated
thanks
Edited by - Zeke on May 23, 2001 8:20:29 AM
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
We are, of course, assuming that you are attempting to find the number of times a string appears in the buffer , and not in the file . Far as I can tell, actually searching the file would be easier (for one thing you can search it on a line-by-line basis). Anyway:
Good luck!
---
Those who can do nothing criticize; those who can, critique.
Edited by - Oluseyi on May 23, 2001 8:50:45 AM
|
Good luck!
---
Those who can do nothing criticize; those who can, critique.
Edited by - Oluseyi on May 23, 2001 8:50:45 AM
Thanks very much I can understand what you are doing but i think z needs to be initialised to 0 not 1. Thanks again.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
I initialized z to 1 because the first character is tested for before the incrementation loop. Also, nSize will hold the actual number of characters in searchstring, not the number less one. But the correct method is the one that produces correct results. In other words, test and figure out what yields proper results.
Good luck!
---
Those who can do nothing criticize; those who can, critique.
Good luck!
---
Those who can do nothing criticize; those who can, critique.
strstr returns the address of the first occurrence. Why not use this address when continuing your search?
nfound will be the number of times searchstring is present in buffer. Make sure buffer is terminated with a null character, otherwise strstr will search past the end of the buffer!
HTH
// get size of search stringint size = strlen(searchstring);// 0 occurrences found yetint nfound = 0;// set start address for search to start bufferchar* start = buffer;// search for first occurrencechar* foundat = strstr(start, searchstring);while (foundat){ // increase number of occurrences ++nfound; // set start address for next search start = foundat + size; // search next occurrence foundat = strstr(start, searchstring);}
nfound will be the number of times searchstring is present in buffer. Make sure buffer is terminated with a null character, otherwise strstr will search past the end of the buffer!
HTH
Some useful C++ links:Free multiplatform ANSI C++ Standard Library implementationVisual C++ 6.0 STL fixesVisual C++ 6.0 noncompliance issuesC++ FAQ Lite
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement