Partial String matching It's doing the opposite!

This is a discussion on Partial String matching It's doing the opposite! within the C Programming forums, part of the General Programming Boards category; I am using strstr to do a partial string match on a linked list. Problem is it is return the ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    17

    Partial String matching It's doing the opposite!

    I am using strstr to do a partial string match on a linked list. Problem is it is return the opposite of what i want.

    say i have various words to compare. These include

    disposed
    dispose
    is

    if i search for "dispose"

    it returns
    dispose
    is

    if i search for "disposed"

    it returns
    disposed
    dispose
    is

    if i search for "is"

    it returns
    is

    But i want it to do the opposite. If i search for "is" i want is, dispose and disposed.

    Any ideas?

    Code:
    /* Structure definitions. */
    typedef struct wordStruct* WordTypePtr;
    
    typedef struct wordStruct
    {
       char* word;
       unsigned count;
       WordTypePtr nextWord;
    } WordType;
    
    typedef struct indexStruct
    {
       WordTypePtr headWord;
       WordTypePtr longestWord;
       WordTypePtr mostCommonWord;
       unsigned totalWords;
       unsigned uniqueWords;
    } IndexType;
    Code:
    IndexType index;
    
    searchIndex(&index);
    Code:
    00000001 void searchIndex(IndexType* index)
    00000002 {
    00000003     char *myString[MAX_SIZE+2];
    00000004     printf("\nIndex Search\n");
    00000005     printf("------------\n");
    00000006     printf("\nEnter a word:");
    00000007     fgets(*myString, MAX_SIZE+2, stdin);
    00000008     
    00000009 
    00000010     WordType *root,*conductor;
    00000011     root = (WordType *)malloc(sizeof(WordType));
    00000012     root->nextWord=index->headWord;
    00000013     root->word=NULL;
    00000014 
    00000015     conductor=root;
    00000016 
    00000017     conductor = conductor->nextWord;
    00000018     if ( conductor != 0 ) { 
    00000019         while ( conductor->nextWord != 0 ) {
    00000020             if (conductor->word!=NULL){
    00000021                 /*printf ("Returned String 1: %s\n", strstr (*myString, conductor->word));
    00000022                     if (strstr (*myString, conductor->word)==NULL){*/
    00000023                 if (strstr (*myString,conductor->word)==NULL){
    00000024                 }else{
    00000025                     printf(conductor->word);
    00000026                     printf ("\t(%d)\n", conductor->count);
    00000027                 } 
    00000028             }
    00000029             conductor = conductor->nextWord;
    00000030         }
    00000031         
    00000032     }
    00000033
    Hope my code is cleaner this time

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Isn't line 17, 18 and 20 the same? Can't they all be deleted, since the while loop on 19 covers the same logic?
    Last edited by Adak; 08-17-2010 at 09:06 AM.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You might want to take a closer look at how strstr() works.
    Quote Originally Posted by Reference
    Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is an approximation of your algorithm. The asterisk in front of mystring - is that working for you?

    I can't get it working in this code:

    Code:
    #include <stdio.h>
    
    
    int main() {
      int i, j, n; 
      char *words[30]={"disposed", "displaced", "diagnose", "disease", "is"};
    
      printf("\n\n");
      i=0;
      while(words[i]) {
        if(strstr(words[i], "is"))  //no asterisk in front of "words"
          printf("\n %s", words[i]);
        ++i;
      }
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar();
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    17
    hi Adak,

    Thanks, that produces the exact result. My issue know is you are using the word[] and i am pulling the word form the linked list.

    I can not seem to get it to do the same.

    I will keep thinking.
    Thanks for the help so far!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ollie, you missed thel point of my post.

    You need to drop the asterisk in your array that you are using to hold the "goal" string, in. Use the index, as I have shown in my program. Since you have just one word, you can use just the name of the array, and no index.

    The fact that you're using a linked list is irrelevant. You're not using strstr() properly.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    17
    ah ok. i see what your saying. cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 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