Search for case insensensitive strings within a .txt file

This is a discussion on Search for case insensensitive strings within a .txt file within the C Programming forums, part of the General Programming Boards category; I mostly have this program completed but there is one problem i cannot figure out, this programs asks the user ...

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

    Search for case insensensitive strings within a .txt file

    I mostly have this program completed but there is one problem i cannot figure out, this programs asks the user to search for a word, next the user is asked which file to open, then the program is to search the .txt file for any traces for the word that has been searched, whether it its upper case or lower case.

    However in my example with the supplied .txt file, i try to search for the word "Peter" i get the following

    Code:
    Please enter the word you would like to search for.
    peter
    Please enter the filename to read from.
    words.txt
    Word found: THis is a sentence
     Line: 1
    
    Word found: peter how many words does it contain
     Line: 2
    Kinda weird if you ask me lol.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void stringtolower(char * str);
    
    int main()
    {
       char searchword[100];
       char filename[100];
       char word[100];
       char originalword[100];
    
       printf("Please enter the word you would like to search for.\n");
       scanf("%s", searchword);
       stringtolower(searchword);
       printf("Please enter the filename to read from.\n");
       scanf("%s", filename);
    
       FILE * fptr;
       fptr = fopen(filename, "r");
    
       if (fptr == NULL)
       {
          printf("Error file was not opened correctly\n");
          exit(1);
       }
    
       else
       {
          int lineNumber = 1;
          while(fgets(word, 100, fptr) != NULL)
          {
             strcpy(originalword, word);
             stringtolower(word);
             if (strcmp(word, searchword) ==1)
             {
    
                printf("Word found: %s Line: %d\n\n", originalword, lineNumber);
             }
             lineNumber++;
          }
       }
       fclose(fptr);
    
       return 0;
    }
    
    void stringtolower(char * str)
    {
       int i;
    
       for (i=0; i < strlen(str); i++)
       {
          if (str[i] >= 'A' && str[i] <='Z')
          {
          str[i] = str[i] + ('a' - 'A');
          }
       }
    }
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should re-examine your use of strcmp, specifically what it means when it returns "1".

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    yes, i see what you mean, but its (my fault from not telling you at first post) supposed to search for the word "peter" and in the .txt file thier is one called "PetER" now the program goes to the stringtolower function and scans the document and converts all uppercase letters into lower case, when the function is completed strcpy copies the word into another string so it retains its original lettering in this case "PetER" then the rest of the program is only meant to output the lines that have the word peter in it without displaying the rest of the lines during output

    An example:
    Code:
    Please enter the word you would like to search for.
    PetER
    Please enter the filename to read from.
    words.txt
    Word found: this is a sentence
     Line: 1
    
    Word found: peter how many words does it contain
     Line: 2
    
    Word found: if
     Line: 3
    
    Word found: add more words peter
     Line: 5
    
    Word found: how about this program
     Line: 6
    While it should do:
    Code:
    Please enter the word you would like to search for.
    PetER
    Please enter the filename to read from.
    words.txt
    Word found: peter how many words does it contain
     Line: 2
    
    Word found: add more words PetER
     Line: 5

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by greg677 View Post
    yes, i see what you mean, but its (my fault from not telling you at first post) supposed to search for the word "peter" and in the .txt file thier is one called "PetER" now the program goes to the stringtolower function and scans the document and converts all uppercase letters into lower case, when the function is completed strcpy copies the word into another string so it retains its original lettering in this case "PetER" then the rest of the program is only meant to output the lines that have the word peter in it without displaying the rest of the lines during output

    An example:
    Code:
    Please enter the word you would like to search for.
    PetER
    Please enter the filename to read from.
    words.txt
    Word found: this is a sentence
     Line: 1
    
    Word found: peter how many words does it contain
     Line: 2
    
    Word found: if
     Line: 3
    
    Word found: add more words peter
     Line: 5
    
    Word found: how about this program
     Line: 6
    While it should do:
    Code:
    Please enter the word you would like to search for.
    PetER
    Please enter the filename to read from.
    words.txt
    Word found: peter how many words does it contain
     Line: 2
    
    Word found: add more words PetER
     Line: 5
    You still don't seem to understand the return values of strcmp. I suggest you look up this function online and stop assuming it returns 1 when two strings are equal.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    ok managed to get it working, but the unnecessary lines are still showing, how can i only display the ones that have the word i searched for? thanks for the help so far.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Considering you must have some way of knowing if you have found a match, then you should add in there the part that prints that line, right there. Basically, if you're already doing something like so:
    Code:
    if( findmatch( word, line ) == YAY )
    {
        increasesomecounter++;
    }
    Then you should change it to something like this:
    Code:
    if( findmatch( word, line ) == YAY )
    {
        increasesomecounter++;
        print out whatever you need to print( ie: print line )
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 08:23 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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