从键盘上输入N个字符串(约定:每个字符串中字符数≤80字节),对其进行升序排序并输出。
函数接口定义:
void sort_Str(char **p);
裁判测试程序样例:
#include<stdio.h> #include<string.h> #define N 5 void sort_Str(char **p); int main(void) { int i; char *pstr[N], str[N][81]; for (i=0; i<N; i++) { pstr[i] = str[i]; } for (i=0; i<N; i++) { gets(pstr[i]); } sort_Str(pstr); for (i=0; i<N; i++) { printf("%s\n", pstr[i]); } return 0; } /* 请在这里填写答案 */
输入样例:
hello
My
Friend
Are you ok?
help me!
输出样例:
Are you ok?
Friend
My
hello
help me!
void sort_Str(char **p)
{
int i,j;
char t[81];
for(i=0;i<N;i++)
{
for(j=i;j<N;j++)
if(strcmp(p[i],p[j])>0)
{
strcpy(t,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],t);
}
}
}
字符串的排序需要用到函数,交换也需要函数,排序的思想类似于数组。
本文介绍了一种使用C语言实现的字符串排序方法,通过比较字符串大小进行升序排列,并提供了完整的代码示例。
1371

被折叠的 条评论
为什么被折叠?



