Thread: strcmp and ascii value

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    strcmp and ascii value

    Is there any way to make the strcmp ignore the difference between capital letters and lower case? My sort is working but it is putting all the words with capitals first then the lower case.

    Thanks

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    You could put everything in uppercase to begin with (toupper or tolower, depending on how you feel)

  3. #3
    Unregistered
    Guest
    Use strcasecmp() (/stricmp).

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Use strcasecmp() (/stricmp).

    These functions are not part of the ANSI standard.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    If you don't mind writing you own compare function, you can get that functionality:

    Code:
    int
    mystrcmp(char *o, char *n)
    {
    	while (*o)
    	{
    		if (tolower(*o) == tolower(*n) )
    		{
    			o++;
    			n++;
    		}
    		else
    		{
    			return(-1);
    		}
    	}
    
    	if (! *n)	// if *n isn't null when *o is, the strings are different lengths
    		return(0);
    	else
    		return(-1);
    	
    }
    Just call mystrcmp as you would call strcmp.

    clu82

  6. #6
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    no real need
    use the tolower() function ( i think its defined in string.h)
    this will convert the string to lower case. If you wish to preserve case - copy the string convert that to lower check it and then use your original which still be in its original case
    Monday - what a way to spend a seventh of your life

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    tolower() will convert 1 character at a time.

    Won't you will still need to write some kind of loop to check the string 1 character at a time?


    clu82

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM