String Search

This is a discussion on String Search within the C Programming forums, part of the General Programming Boards category; Hey i have a program that reads the contents of a file into an array...now i want to extend this ...

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

    String Search

    Hey i have a program that reads the contents of a file into an array...now i want to extend this program so that when i type in search "word" it prints out the line in the text that has the "word in it.
    The code i have for opening the file and just printing it is:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 100
    
    int main(int argc, char *argv[]) {
    
       FILE *input;
       char run[1000];
       int count;
       int i=0;
       char string[1000][SIZE];
    
    
          input=fopen(argv[1], "r"); 
          if(input==NULL) {
             printf("No such file\n");
          } else {
             while((fgets(string[i], SIZE, input)!=NULL)) {
                i++;
             }
             count=i;
             while (strcmp(run, "quit")!=0){
             printf("Enter command > ");
             scanf("%s", run);        
                if (strcmp(run, "print")==0){
                   for(i=0;i<count-1;i++){
                      printf("%d %s", i, string[i]);
                   }
                  }
                }
              }
             
    return 0;
           }
    this is what i want to extend my program into:
    For example the file has these lines:
    1 I love programming
    2 I hate programming
    3 Programming runs the world

    then in my program i type in "search love"
    the program should print:
    1 I love programming

    I am thinking that i need to store the word i am searching for into a temp array and then used the strcmp function to compare this "word" with all the ones in the file and then printing out the required lines...I do not know if this logic if right..and even if its i usually have trouble converting the logic into C syntax..
    Any help will be greatly appreciated..

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
             while (strcmp(run, "quit")!=0){   /* 'run' is un-initialized */
    Compile with all warnings enabled. You can just use if .. else construct.

    Code:
      /* not tested */
    if( strcmp(run,"print") == 0 ) {
    /* ... */
    } else if( strcmp(run,"search") == 0 ){
       char search[100];  
       scanf("%s",search);
      /* ... */
    }

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    ok so what you are suggesting is that when search is typed in, the program initializes another array called search...
    then it scans in the input presented by the user into this array...

    Um i don't think that's what i was looking for because i want to type in the phrase:
    search "XXXX" (XXXX is just a random word)
    the program should only search the "XXXX" part of the program.
    So what i was thinking was that i should copy the input (search "XXXX") into another string but start the counter at 8 so that only the "XXXX" is copied into this string...
    Then i want the program to search for any matches of "XXXX" in the file and everytime it finds a match i need it to print out the line it found the match in....

    but i don't know how to properly use C to work this out..

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Try
    Code:
     char buf[100];
      scanf("%s",buf);
      printf("%s\n",buf);
      scanf("%s",buf);
      printf("%s\n",buf);
      /* with input string  
         search noob
       */
    Read scanf documentation. You should read manual of all the functions you use.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Bayint Naung View Post
    Try
    Code:
     char buf[100];
      scanf("%s",buf);
      printf("%s\n",buf);
      scanf("%s",buf);
      printf("%s\n",buf);
      /* with input string  
         search noob
       */
    Read scanf documentation. You should read manual of all the functions you use.
    What does this have to do with what he is trying to accomplish?
    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.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    There is a standard C function strstr() that you can use to find one string in another.

    You could split and command (search) and command parameter ("love") into separate inputs. It would make your program modal. Typing in "search" would put it into search-mode that would allow you to enter search terms and output the result. Typing a specific character sequence would take you out of the search mode.

    If that is not possible, and you require the command parameter approach, you should look at the strtok(), that you can use to split input in the command and parameter part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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