AlpHaNumeric sOrtinG

May 4, 2009 at 1:22am
hey guys, i need help about this problem....
i am sorting a number of names, but if i inputted a lower case and a upper case value, it will not be equal.
1
2
3
4
5
6
example:
Enter number of names; 3
Enter names:
albert
Albert
Nel
The sorted names:
Albert
Nel
albert

i need help.. this is my codes..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int y,x,n,j;
    char names[20][20],temp[20];
    
    puts("Enter number of students: ");
    scanf("%d",&n);
    
    j = n + 1;
    puts("Input the names");
    for(x = 0;x < j;x++)
        gets(names[x]);
    
    for(x = 0;x < j;x++)
    { 
       for(y = x + 1;y < j;y++)
       {
             if(strcmp(names[x],names[y])>0)
             {
                strcpy(temp,names[x]);
                strcpy(names[x],names[y]);
                strcpy(names[y],temp);
             }
       }
    }      
    printf("--------------------\n");
    puts("The sorted names");
    for(x = 0;x < j;x++)
        puts(names[x]);
    
    system("pause");
    return 0;
}
May 4, 2009 at 2:12am
I don't think there's a case-insensitive version of strcmp(). You'll have to either convert all the strings to the same case before starting, or write your own version of strcmp().
May 4, 2009 at 12:24pm
int strcasecmp( const char* s1, const char* s2 );



May 4, 2009 at 12:53pm
Is that standard?
May 4, 2009 at 3:25pm
The man page only says BSD 4.4 as opposed to the man page for strcmp which
lists SVID 3, POSIX, BSD 4.3, ISO 9899.

On the other hand the man page does not say anything about being non-portable.

Hmm....
May 4, 2009 at 3:32pm
jeysel, just write your own strcmp that is case-insensitive. No reason to use something non-standard for something so simple.
Topic archived. No new replies allowed.