Print words ending with 'ing' from a text file

This is a discussion on Print words ending with 'ing' from a text file within the C Programming forums, part of the General Programming Boards category; Hi, I am hoping someone can point me in the correct direction, I am to prompt to read a text ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    England
    Posts
    4

    Print words ending with 'ing' from a text file

    Hi, I am hoping someone can point me in the correct direction,
    I am to prompt to read a text file, then search through the file and print all the strings ending with 'ing'....I am having trouble with the final part of this, the searching form the ing strings, I have tried all I can think of, but to no avail... I have below the steps up to the finding the ing words, but no matter what I try I cannot get them.... all help appreciated

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    England
    Posts
    4

    so far

    so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define GROWBY 4096
    
    
    char ** readfile(char *inputfile, int *nolines);
    void stringsearch(char *data[], int nolines);
    
    
    
    
    
    
    int main (void)
    {
        char input[100];
        int nolines = 0;
        char ** filevalues; 
    
    
        printf("Please enter the name of the file you wish to have checked>> ");
        scanf("%s", &input);
    
    
    filevalues = readfile(input, &nolines);
    
    
    stringsearch(filevalues, nolines);
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
    
    
    }
    
    
    char **readfile(char *inputfile, int *nolines)
    {
        FILE * inputfilePtr;        /* File pointer */
        char ** invalues;        /*array for storing values fromt he file*/
        char temp[100];            /*to allocate the values from the file*/
        int x = 0;
        inputfilePtr = fopen(inputfile, "r"); /* allocate pointer the file data*/
    
    
        while (!inputfilePtr)
        {
            fprintf(stderr, " Cannot open file %s, please try again>> ", inputfile);
            scanf("%s", inputfile);
        }
    
    
    
    
        invalues = (char**)malloc(sizeof(char*) * GROWBY); /* allocate dynamic memory*/
    
    
        while ((NULL != fgets(temp, sizeof(temp), inputfilePtr)))   /*read values from the file*/
        {
                invalues[x++] = strdup(temp);
    
    
                if ((x % GROWBY) == 0) /*loop for creation of space*/
                {
                      invalues = (char**)realloc(invalues, sizeof(char*) * (GROWBY + x));
                }
          }
     
          *nolines = x;        
          fclose(inputfilePtr); /*close file*/
          
     
          return invalues;
    }
    
    
    
    
    void stringsearch(char *data[], int nolines)
    
    
    {
        int a;
        char test[] = {'ed'};
        int z;
        char *bet[]= {0};
        FILE * help;
        
    
    
        for (a=0; a< nolines; a++)
        {
         int b = 0; 
            b = strlen(data[a]);
            
     
    
    
            
    
    
            
            }
            
        }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's "too big a eat", to quote Rodney Allan Wippy.

    Throw out the malloc(), you don't need it and GROWBY along with it, and think simple:

    1) take in a row of text into a BUFSIZ char buffer

    2) look for the substring "ing" in that row of text. strstr() is your tool, since it searches for a string, inside a string.

    3) If strstr() finds a match, you back up to the nearest space, and then start putting in your letters into a smaller array, until you get to the next space or comma, or newline (at the end of the row).

    Include the ctype.h header file, and use isalpha() for the test on finding the beginning and the end of the word. Makes a great while() loop.

    Sound good?

    oh, and you can use
    Code:
    while((fgets(buffer, BUFSIZ, filePointer)) != NULL) {
          //and all your code in here
    }
    To read in all the rows from the text file.
    Last edited by Adak; 10-16-2011 at 12:47 PM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    There is an easy way to read 'words' from a textfile, if 'word' is defined as splitted by all whitespaces.
    You dont need to read all lines in your allocated line-array, your readfile function should scan all words and put it out if your 'ing' check is successful

    Code:
    char word[100];
    ...
    while( 1==fscanf(inputfilePtr,"%99s",word) )
      if( strlen(word)>2 && !strcmp(word+strlen(word)-3,"ing") )
        printf("found: %s\n",word);
    ...
    It should be the easiest way for your usecase.
    Last edited by BillyTKid; 10-16-2011 at 03:21 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh, that's clever Billy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to print a data file with words and numbers
    By Weyoun92 in forum C Programming
    Replies: 6
    Last Post: 05-06-2011, 07:26 AM
  2. Organize words from a text file
    By doia in forum C Programming
    Replies: 8
    Last Post: 06-20-2010, 10:09 PM
  3. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 11:35 AM
  4. counting words in a text file...
    By dalguy2004 in forum C Programming
    Replies: 4
    Last Post: 09-23-2003, 10:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21