12 Replies - 725 Views - Last Post: 03 December 2010 - 12:26 PM Rate Topic: -----

#1 ArthurCamara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-March 10

A function that returns how many times a string is present in a file

Posted 01 December 2010 - 06:09 PM

Hello guys!
So, i`ve been reading a couple of others topics around here, and some of them showed a couple of ideas that i could use to implement the function.
Basic, it`s a function that receives a char* string and a FILE* file, and must return how many times the string appears in the file.
Initially, i put the whole file in a buffer and used strstr()!=NULL to find the string within it, but strstr() just returns ONE pointer to wherever the string is. So, the big question is:
Is there anyway that I can use strstr recursively or, like strtiok, starting from where the last call stopped?
Thanks very much for your help!

P.S.: This program is in pure C, just in case ;)

This post has been edited by ArthurCamara: 01 December 2010 - 06:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: A function that returns how many times a string is present in a file

#2 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: A function that returns how many times a string is present in a file

Posted 01 December 2010 - 06:38 PM

Try this source code and tell me if it works.

#include <stdio.h>

int main(int argc, char *argv[])
{
	FILE *fp = fopen(argv[1], "r");
	int counter = 0;
	char wordChoice[50];
	if(fp)
	{
		puts("What word are you searching for?:");
		gets(wordChoice);
		char buffer[2024]; //Sorry for this.
		while(fgets(buffer, 2024, fp) != NULL)
		{
			if(strstr(buffer, wordChoice))
			{
				//printf("%s\n", buffer);
				counter++;
			}
		}
	}
	else
	{
		printf("Error: File doesn't exist!");
	}
	printf("That word appears %d times", counter);
	fclose(fp);
	return 0;
}



This post has been edited by Slumdog: 01 December 2010 - 06:40 PM

Was This Post Helpful? -1
  • +
  • -

#3 ArthurCamara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-March 10

Re: A function that returns how many times a string is present in a file

Posted 01 December 2010 - 06:49 PM

Indeed, after a few modifications (basically, adapting it to my code) it did worked!
Thank you VERY MUCH! =D
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,213
  • Joined: 23-August 08

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 04:52 AM

Slumdog, we try not to just hand people full code without the user demonstrating some effort. Please do not do this again.
Was This Post Helpful? 0
  • +
  • -

#5 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 06:23 PM

View PostJackOfAllTrades, on 02 December 2010 - 03:52 AM, said:

Slumdog, we try not to just hand people full code without the user demonstrating some effort. Please do not do this again.


Sorry, I won't repeat it, again. I didn't help anyone on this forum after I made this account, so I was eager.
Was This Post Helpful? 0
  • +
  • -

#6 ArthurCamara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-March 10

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 07:54 PM

Hello guys! Yesterday I showed up with a question that was quickly solved. Pretty much, I had to make a function to count how many times a string shows up in a file. The problem is: With the solution I'm using by now, if I search, for instance, for the word "World" in a file, I will just count if the token is just "World". if it is "World's", it won't add to the counter. Anyway that I can solve this? Here is my code so far:
int SearchCP(FILE* corpus, char* Word)
{
   char buffer[2024];
   int counter=0;

   while(fgets(buffer, 2024, corpus)!=NULL)
   {
       if(strstr(buffer, Word))
       {
           counter++;
       }
   }
   return counter;
}

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 08:16 PM

So that's the code that Slumdog handed you yesterday. What have you done to try to solve this?
Was This Post Helpful? 0
  • +
  • -

#8 ArthurCamara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-March 10

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 08:21 PM

Well, right now I'm trying a completly different approach, using memcmp to compare the buffer and the string, but it can't find any occurence of the string within the file. I'm thinking about trying to use something with strtok, but I'm not very sure if it will work
Was This Post Helpful? 0
  • +
  • -

#9 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 08:48 PM

View Postr.stiltskin, on 02 December 2010 - 07:16 PM, said:

So that's the code that Slumdog handed you yesterday. What have you done to try to solve this?


'that Slumdog'? Gee. Thanks. ;P

Anyway, strtok doesn't search for strings. It tokenizes the given string.
Was This Post Helpful? 0
  • +
  • -

#10 ArthurCamara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-March 10

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 08:55 PM

View PostSlumdog, on 02 December 2010 - 07:48 PM, said:

Anyway, strtok doesn't search for strings. It tokenizes the given string.

indeed, but I can tokenize the string with " '\n /*etc etc*/ " and them use strcmp with the tokenized string and the one i'm looking for. pretty much like this:
int SEarchCP(FILE* corpus, char* Word)
{
    char* Aux;
    char buffer[2024];
    int counter=0;
    char tokenizators[] =" !\"#$%&\\'()*+,-./:;<=>?@[\\]^_`{|}~\n";


    while(fgets(buffer, 2024, corpus)!=NULL)
    {
        Aux = strtok(buffer, tokenizators);
        while(Aux!=NULL)
        {
            if(!strcmp(Aux, Word))
            {
                counter++;
            }
            Aux = strtok(NULL, tokenizators);
        }
    }
    return counter;
}

Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A function that returns how many times a string is present in a file

Posted 02 December 2010 - 09:21 PM

How does that help you? That won't find World if the corpus contains Worldwide.

What was wrong with memcmp?

edit: another thing to consider is do you want "world" to match "World" or "netherworld" or "unworldly"?

This post has been edited by r.stiltskin: 02 December 2010 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,213
  • Joined: 23-August 08

Re: A function that returns how many times a string is present in a file

Posted 03 December 2010 - 04:35 AM

Merged duplicates. Please do NOT create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#13 budfox  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-December 10

Re: A function that returns how many times a string is present in a file

Posted 03 December 2010 - 12:26 PM

The strstr-example works not for more than one pattern in A line and not if the pattern is a substring in an other word.
Better should work:
int nums(FILE *f,const char *pat)
{
  int n=0;
  char line[1000];
  while( fgets(line,1000,f) )
  {
    int i=0;
    char *p=line,word[1000];
    while( sscanf(p+=i,"%s%n",word,&i) )
      if( !strcmp(word,pat) ) ++n;
  }
  return n;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1