Thread: Search of string array isn't working correctly

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    34

    Search of string array isn't working correctly

    Hi all -

    I've written this code to search an array of strings for a given string. Here it is -

    Code:
     
    
    
    /*  search.c  */ 
    
    /*  Function to search an array of strings */
    /*  to find a given string. */ 
    
    /*  This code is released to the public domain. */ 
    /*  "Share and enjoy...."  :)    */ 
    
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h> 
    
    
    /* Array of strings. */ 
    static const char *kw_strings[] = { 
       "select", "from", "where", "and", "or", "not", "in", "is", "null" 
        } ; 
       
        
    /*  Search function.  */ 
    void search(const char *arr[], char *str) { 
        int len = sizeof(arr) / sizeof(arr[0]) ; 
        int i;
        
        for (i=0; i<len; i++) { 
            if ( !strcmp(arr[i] , str ) )  {   
                printf("%s \n", "Found"); 
         } 
            else  { 
                printf("%s \n", "Not Found");        
        } 
        
     }  /* For */     
    
    }  /* search */ 
    
    
    int main() { 
    
      search(kw_strings , "select") ;   
      
      search(kw_strings , "from") ;   
      
      search(kw_strings , "foobar") ; 
      
      search(kw_strings , "I like moose") ; 
           
      return 0; 
        
    }
    This *should* result in the first two searches succeeding.
    However, I'm getting the first one succeeding and then three fails. Any help on why this is is very much appreciated!

    - Andy ( latte123 )

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    You need to pass the size of the array along with the array, rather than calculating it in the search() function, as arr in the search() function is just a pointer. search() cannot determine the size of the "array" being passed.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    34
    Quote Originally Posted by rstanley View Post
    You need to pass the size of the array along with the array, rather than calculating it in the search() function, as arr in the search() function is just a pointer. search() cannot determine the size of the "array" being passed.
    Ahhh...... thanks for that, rstanley! Should be an easy fix.....
    Thanks again, bye for now -
    - Andy

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    34
    Hi again all -

    I've got the code working perfectly now. The search now returns an int - code follows -

    Code:
     
    
    /*  search.c  */ 
    
    /*  Function to search an array of strings */
    /*  to find a given string. */ 
    
    /*  This code is released to the public domain. */ 
    /*  "Share and enjoy...."  :)    */ 
    
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h> 
    
    
    
    /* Array of strings. */ 
    char *kw_strings[] = { 
       "select", "from", "where", "and", "or", "not", "in", "is", "null" 
        } ; 
       
        
    /*  Search function.  */ 
    int search(char *arr[], char *str) { 
        
        int i;  
        int found_match;
        
        for (i=0; i<9; i++) { 
            if ( !strcmp(arr[i] , str ) )  {   
                found_match = 1;        
                break; 
        }   else found_match = 0;    
     }  /* For */     
    
        return found_match; 
    }  /* search */ 
    
    
    
    int main() { 
    
    
      int i = search(kw_strings , "select") ;   
      
      int j = search(kw_strings , "from") ;   
      
      int k = search(kw_strings , "null") ;   
      
      int l = search(kw_strings , "foobar") ; 
      
      int m = search(kw_strings , "I like moose") ; 
     
      printf("%d %d %d %d %d \n", i,j,k,l,m); 
            
      return 0; 
        
    }
    The first three searches succeed and return 1, as expected. Woohoo!

    - Andy ( latte123 )

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    No, you should make the search() function generic by PASSING the array dimension!!!

    Instead of:
    Code:
    int search(char *arr[], char *str)
    {
    ...
    }
    You should make it:
    Code:
    int search(char *arr[], int dim, char *str)
    {
    ...
    }
    I'll let you fill in the code. This way you may pass different arrays to the function, with different sizes.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    34
    Hi rstanley -

    Done - thanks for that! Here's the code for anyone interested -

    Code:
    
    /*  search.c  */ 
    
    /*  Function to search an array of strings */
    /*  to find a given string. */ 
    
    /*  This code is released to the public domain. */ 
    /*  "Share and enjoy...."  :)    */ 
    
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h> 
    
    
    
    /* Array of strings. */ 
    char *kw_strings[] = { 
       "select", "from", "where", "and", "or", "not", "in", "is", "null" 
        } ; 
       
        
    /*  Search function.  */ 
    int search(char *arr[], int dim, char *str) { 
        
        int i;          
        int found_match;
        
        for (i=0; i<dim; i++) { 
            if ( !strcmp(arr[i] , str ) )  {   
                found_match = 1;        
                break; 
        }   else found_match = 0;    
     }  /* For */     
    
        return found_match; 
    }  /* search */ 
    
    
    
    int main() { 
     
      int i = search(kw_strings, 9, "select") ;   
      
      int j = search(kw_strings, 9, "from") ;   
      
      int k = search(kw_strings, 9, "null") ;   
      
      int l = search(kw_strings, 9, "foobar") ; 
      
      int m = search(kw_strings, 9, "I like moose") ; 
     
      printf("%d %d %d %d %d \n", i,j,k,l,m); 
            
      return 0; 
        
    }
    Thanks for your help!
    - Andy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string array search
    By Svetlin in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2010, 03:06 AM
  2. String search not returning correctly?
    By Blackroot in forum C++ Programming
    Replies: 5
    Last Post: 08-18-2008, 06:41 PM
  3. search numbers in an inputed string [array of strings]
    By zattara13 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 06:11 AM
  4. binary search function on string array
    By gandolf989 in forum C Programming
    Replies: 6
    Last Post: 10-02-2007, 01:47 PM
  5. string array text search
    By ery in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2001, 03:00 PM

Tags for this Thread