Thread: Counting occurrences of a word in a string

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Counting occurrences of a word in a string

    I'm trying to write a program that will take in several lines of text and then take in a target word to look for in the lines of text. I need to return how many times the target word appears in the lines of text. I have everything but the function to find the number of occurrences of the target word. I've been working on it for a while and can't find a way to do it.



    Code:
    #include <stdio.h>
    #include <string.h>
    
    int analyzeLine(char text[], char target[]);
    // Function for analyzing lines of text
    
    int main(void)
    {
        int i = 0;            // Variable for progressing through a for loop
        int lines;            // Number of lines to be analyzed
        int occurrences = 0;
        int sum = 0;
        char repeat = 'y';    // Used to prompt if the program should be repeated
        char text[81];        // String for lines of text
        char target[100];
        
        while(repeat == 'y')
        {
           printf("Please enter the target word: ");
           fgets(target, sizeof(target), stdin);
           printf("Please enter the number of lines to be analyzed: ");
           scanf("%d", &lines);
           fflush(stdin);
           
           // Input and analyzation of the lines of text
           
           for(i=0; i<lines; i++)
           {
              printf("\n\nLine #%d:\n", i+1);
              fgets(text, sizeof(text), stdin);
              sum = analyzeLine(text, target);
              occurrences += sum;
           }
           
           printf("\n\n");
           
           // Printing the number of occurrences to the screen
           
           printf("The target occurs %d times\n", occurrences);
           
           // Prompt for repeating of program
           
           printf("\nWould you like to analyze more lines?");
           printf("\nEnter Y for yes or N for no: ");
           scanf(" %c", &repeat);
           repeat = tolower(repeat);
           fflush(stdin);
        }
        
        system("pause");
        return 0;
    }
    
    int analyzeLine(char text[], char target[])
    {

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Check strstr() function from string.h.

    Or, you could write your own function - simply iterate the line from begining to end - store each word in a temp array, when you get to first isspace() char check if the word matches the one you're looking for. If so, increment the counter, otherwise continue searching the line.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Something like this.

    Code:
    int analyzeLine(char text[], char target[])
    {
        char *p;
        char temp[100];
        char *t = temp;
        int state, in = 1, out = 0;
        int occurence = 0;
        
        state = out;
        for(p = text; *p; p++) {
              if(isspace(*p)) {
                 if(state == in) {
                     *t = '\0';
                     t = temp;
                     if(strcmp(temp, target) == 0)
                        occurence++;
                 }
                 state = out;
              } else {
                 *t++ = *p;
                 state = in;
              }
        }
        *t = '\0';
        if(strcmp(temp, target) == 0)
           occurence++;
        return occurence;
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    RTM on regcomp() and regexec() especially the section about rm_so and rm_eo.
    The word becomes the pattern you're looking for ie the 2nd arg to regcomp().
    Create a variable of type regmatch_t which becomes the 4th arg to regexec().
    Call regexec() and if there's a match (returns zero), increment a found counter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  2. Error counting occurrences
    By nick048 in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 02:08 PM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

Tags for this Thread