你看一下把。这个程序就是给多个字符串排序的。
比较字符串大小用 strcmp()
从键盘获取字符串用gets()要比sanf()好。
- #include<stdio.h>
- #include<string.h>
- int main(){
- //定义一个二维数组,存储字符串。
- char str[10][100];
- //定义一个临时字符串.
- char temp[100];
- //从键盘获得10个数组。
- for(int i = 0;i < 10;i++){
- printf("请输入字符串%d:",i+1);
- gets(str[i]);
- }
-
- //利用第一维排序。
- for(int i = 0;i < 10;i++){
- for(int j = i + 1;j < 10;j++){
- //利用函数strcmp()判断字符串大小。
- if(strcmp(str[i],str[j])>0){
- //利用函数strcpy来交换字符串
- strcpy(temp,str[i]);
- strcpy(str[i],str[j]);
- strcpy(str[j],temp);
- }
- }
- }
-
- for(int i = 0;i<10;i++){
- printf("%s\n",str[i]);
- }
-
- return 0;
- }
复制代码 |