Advertisement

How can i find number of "string"'s in array?

Started by May 23, 2001 07:15 AM
3 comments, last by Zeke 23 years, 8 months ago
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:

    char buffer[20000];   // your filechar *searchstring;   // allow for any string, not just "FILENAME"// initialize searchstring hereint sSize = sizeof(searchstring)/sizeof(char);int x, y = 0, z = 1;  // countersint nFound = 0;       // number of times the string has been foundfor( x = 0; x < 20000; x++){   if( buffer[x] == searchstring[y] )   // first character   {      while( buffer[++x] == searchstring[++y] ) z++;      if ( z == sSize ) nFound++;      // note that x has been incremented, so the loop continues      // properly      y = 0; z = 0;    // gotta reset the counters   }}  


Good luck!

---
Those who can do nothing criticize; those who can, critique.

Edited by - Oluseyi on May 23, 2001 8:50:45 AM
Advertisement
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.
strstr returns the address of the first occurrence. Why not use this address when continuing your search?

// 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

This topic is closed to new replies.

Advertisement