Sort Strings in C

问题描述

The function strlwr() is a function from the Microsoft c library and does not work in the standard c library.
因此linux gcc需要自定义strlwr函数原型。自己写一个strlwr.h原型,放在/usr/inlcude/里面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// /usr/include/strlwr.h
char *strlwr(char *s)
{
char *str;
str = s;
while(*str != '\0')
{
if(*str >= 'A' && *str <= 'Z')
{
*str += 'a'-'A';
}
str++;
}
return s;
}
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
#include <stdio.h>
#include <string.h>
#include <strlwr.h>

int main()
{
int i, j, n;
char str[15][100], str1[15][100], tmp[15];

scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%s", str[i]);
}

for(i = 0; i < n-1; i++)
{
for(j = i+1; j < n; j++)
{
if(strcmp(strlwr(str[i]), strlwr(str[j])) > 0)
{
strcpy(tmp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], tmp);
}
}
}

for(i = 0; i < n; i++)
{
puts(str[i]);
}
}