Thread: Searching for partial strings

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Question Searching for partial strings

    I have an array of structures called STUDENTs that has student names in it, and I need to be able to search this array for partial student names and get back full student records.

    For example I need to be able to search for "Mercury, Fred" and get back a student record as well as be able to search for "M" and get back all names starting with "M."

    This is the function I have for it:

    Code:
    int nSearch(STUDENT ary[], int size, char key[])
    {
    	int i;
    	int results = 0;
    	
    	for(i = 0; i < size; i++)
    	{
    		if(strncmp(key, ary[i].name, sizeof(key)) == 0) 
    		{
    			printf("\n+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
    			printf("| Student Name        |  ID    | Test 1 | Test 2 | Proj 1 | Proj 2 | Average | Grade |\n");
    			printf("+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
    			printf("| %-20s| %6d |%5d   |%5d   |%5d   |%5d   |%8.2f |   %-2s  |\n", ary[i].name, ary[i].id, 
    				ary[i].s1, ary[i].s2, ary[i].s3, ary[i].s4, ary[i].avg, ary[i].grade);
    			results++;
    		}
    	}
    	if (results > 0) printf("+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
    	if (results == 0) return 0; //no match
    }
    Obviously not working. Anyone know what I can go?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    int nSearch(STUDENT ary[], int size, char key[])
    ...
    if(strncmp(key, ary[i].name, sizeof(key)) == 0)
    Remember that key is a pointer (same as writing, char * key). So sizeof() will usually return 4 (or 8 now 64-bit machines are coming popular ). Use strlen() instead which you should call once at the start of the function... ie

    Code:
    int nSearch(STUDENT * arry, size_t n, const char * key)
    {
       size_t len = strlen(key);
       size_t i = 0;
    
       for(i = 0; i < n; i++)
       {
          if(strncmp(arr[i].name, key, len) == 0)
          {
             /* match */ 
          }
       }
    }
    Last edited by zacs7; 12-02-2008 at 08:14 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Quote Originally Posted by zacs7 View Post
    Code:
    int nSearch(STUDENT ary[], int size, char key[])
    ...
    if(strncmp(key, ary[i].name, sizeof(key)) == 0)
    Remember that key is a pointer (same as writing, char * key). So sizeof() will usually return 4 (or 8 now 64-bit machines are coming popular ). Use strlen() instead which you should call once at the start of the function... ie

    Code:
    int nSearch(STUDENT * arry, size_t n, const char * key)
    {
       size_t len = strlen(key);
       size_t i = 0;
    
       for(i = 0; i < n; i++)
       {
          if(strncmp(arr[i].name, key, len) == 0)
          {
             /* match */ 
          }
       }
    }
    worked perfectly. I certainly appreciate it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But isn't strncmp() where n = strlen() of one of the strings, just the same as strcmp() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    But isn't strncmp() where n = strlen() of one of the strings, just the same as strcmp() ?
    No, only if n is the length of the LONGER of the strings. strncmp() will MATCH (return 0) if the strings match upto len, even if the longer string continues. E.g.
    Code:
    strncmp("Allan", "All", 3);
    will return 0.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  3. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  4. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 03:45 PM
  5. Searching through strings...
    By C-Duddley in forum C Programming
    Replies: 1
    Last Post: 09-14-2001, 04:26 PM