Help with search program.

This is a discussion on Help with search program. within the C Programming forums, part of the General Programming Boards category; I'm building a program that searches a tree for a name and then outputs their phone number. The program all ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Help with search program.

    I'm building a program that searches a tree for a name and then outputs their phone number. The program all works well I need to make it ignore the case of the search input when searching for a name.

    I've done this by building a function to make strings lowercase and then calling this function when comparing the strings.

    However, its all gone tits up and I've been raking my brains as to whats not right. Its something to do with the way I've calling my data structure i would suspect.

    Any input would be greatly appreciated.

    Code:
    char *search( Tree *root, entry *what ) {
         if( root == NULL ) {
             return "Not Found";
         } else if( strcmp(makelower(what->name), makelower(root->thisnode.name)) == 0) {
             return root->thisnode.number;
         } else if( strcmp(makelower(what->name), makelower(root->thisnode.name)) < 0 ) {
             return search(root->left, what);
         } else {
             return search(root->right, what);
         }
    }
    
    char *makelower(char *s) {
         int n = strlen(s);
         while (n > 0) {
               if (s[n] <= 90 && s[n] >= 65) {
                        s[n] = s[n] - 32;
               }
         n++;
         }
         return strdup(s);
    }

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    http://www.eternallyconfuzzled.com/tuts/bst1.html

    There's quite a lot of info on BST's over three pages, happy reading!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Do you know that the makelower() function modifies the passed string and returns a copy of the modified string. And you never free the string returned by makelower() ?
    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    i'll give that article a read, cheers

    and no I didnt know that I don't free the string - maybe i should do that?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dacbo
    and no I didnt know that I don't free the string - maybe i should do that?
    SInce you modify the original string it does not make much sense to duplicate the string with strdup(). But if you choose to do so you should free the duplicate.
    Kurt

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    have modified the program as you said which has fixed my problem but I'm now getting segmentation faults with my makelower function

    am I calling and compairing s[n] in the right way?

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry I did not look much at your makelower() function.
    It's completely wrong
    Code:
    char *makelower(char *s) {
         int n = strlen(s);           // n points to the '\0' terminator
         while (n > 0) {              // you loop until n will overflow
               if (s[n] <= 90 && s[n] >= 65) { 
                        s[n] = s[n] - 32;
               }
         n++;
         }
         return strdup(s);
    }
    it should be somethong like this. I chose not to modify the passed string
    Code:
    char *makelower(char *s) {
         char * ret = strdup(s);
         int n = strlen(ret);
        int i = 0;
         while (i  < n) {
               if (ret[i] <= 90 && ret[i] >= 65) { 
                        ret[i] = ret[i] - 32;
               }
             i++;
         }
         return ret;
    }
    But generally it is not a good idea to allocate memory in a function. It's too easy to forget to free the returned pointer.
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    ahah, thanks very much for your help - thats done it

    stupid mistake by the looks of it

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You also shouldn't rely on hard coded numbers to represent your alphabet. If you want to compare it to a character, then do so:
    Code:
    if( foo[ x ] <= 'Z' && foo[ x ] >= 'A' )
    The alphabet is not guaranteed to have 90 be letter foo all the time, and 65 be letter bar.


    Quzah.
    Last edited by quzah; 12-08-2005 at 05:04 PM. Reason: Can't spell for ........ some times...
    Hope is the first step on the road to disappointment.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Nor are letters of the alphabet guaranteed contiguous.
    Nor are lowercase and uppercase letters guaranteed to be 32 elements apart.
    Which is probably why the functions isupper, tolower, etc. are standard.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    yeah makes sense, cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary search program
    By saszew in forum C Programming
    Replies: 2
    Last Post: 11-14-2008, 01:04 AM
  2. An interesting challenge --> A word search program
    By desipunjabi in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 03:30 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM
  5. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 07:33 AM

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