Thread: Help with searching word lists

  1. #1
    Registered User denizengt's Avatar
    Join Date
    Sep 2003
    Posts
    19

    Help with searching word lists

    I have a string read from the KB, say string[].

    I have word lists:

    Code:
    static char *dictSpace[] = 
       {"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ", "Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen ", "Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety ", "Hundred ","Thousand ","Million "};
    
       static char *dict[] ={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine", "Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen", "Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety", "Hundred","Thousand","Million"};

    And I have this function. What I am trying to do, is split the input string into tokens. (Or break up the string into component words) and search each of the word lists using that word. This doesn't work below, and I am not sure why. I want to return -1 when the word isn't found in either word list. Ideally, I want to pass a whole sentence to a function, check each of it's words against those in some wordlists, and if any of the words isn't in the lists, output a message to the user.

    Code:
               
       int searchX(char string[], char dict[], char dictSpace[]){
          char *p;
          p = strtok(string, " ");
          int len = strlen(string);
       
       
          do{
             p = strtok('\0', " ");
             if((strcmp(p, dict) && strcmp(p, dictSpace)) < 0) {
             	printf("Done");
                return -1; /*Not found*/
             } 
             else 
                return 0; /*Found*/
          
          
          
          }while(p);
    
    }

    Any help appreciated!

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    Code:
    int searchX(char string[], char dict[], char dictSpace[]){
          char *p;
          p = strtok(string, " ");
          int len = strlen(string);
       
       
          do{
             p = strtok('\0', " ");
             if((strcmp(p, dict) && strcmp(p, dictSpace)) < 0) {
             	printf("Done");
                return -1; /*Not found*/
             } 
             else 
                return 0; /*Found*/
          
          
          
          }while(p);
    
    }
    first of all, strcmp takes two strings (char *) and compares these.
    You are trying to send the whole char * array to strcmp, and that doesnt work. For every word in the sentence , you have to compare it with every word in the char * arrays. ( dict, dictSpace ).
    Its going to be a loop in a loop.
    The second thing is that if you find the word, you return 0. But if you dont find the word in the comparing, you dont return nothing.
    return -1 is done after you have lopped thrue every word.
    i. o. w. move return -1 outside while(p)
    "Can i really learn this? If i cant, why bother?"

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    This might give you some ideas...
    Code:
    #include <string.h>
    
    #define DICT_SIZE 10
    
    void searchX(char string[], char* dict[])
    {
      char *p = strtok(string, " ");
    
      while (p)
      {
        unsigned int i;
    
        for (i = 0; i < DICT_SIZE; i++)
        {
          if (strcmp(p, dict[i]) == 0)
          {
            printf("found in dictionary: %s\n", p);
            break;
          }
        }
    
        if (i >= DICT_SIZE)
          printf("not found in dictionary: %s\n", p);
    
        p = strtok(NULL, " ");
      }
    }
    
    int main(void)
    {
      char * dict[DICT_SIZE] = {"zero","one","two","three","four",
                                "five","six","seven","eight","nine"};
    
      char string[] = "one three apple seven";
    
      searchX(string, dict);
    
      return 0;
    }
    DavT
    -----------------------------------------------

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Here's a link I found useful in searching strings--

    http://www.ece.iastate.edu/~aluru/bc...String.pdf.pdf

    I guess the real question is:

    Are you looking for a specific pattern of letters within a string, or are you looking for a specific word in a string of words?
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM