Menu Close

C语言练习题:输入几个字符串,把他们按一定顺序排列出来

在下面的程序中,用户将被要求输入一组字符串,程序将按字母升序对它们进行排序和显示。

#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings u are going to enter?: ");
   scanf("%d",&count);

   puts("Enter Strings one by one: ");
   for(i=0;i<=count;i++)
      gets(str[i]);
   for(i=0;i<=count;i++)
      for(j=i+1;j<=count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("Order of Sorted Strings:");
   for(i=0;i<=count;i++)
      puts(str[i]);
   
   return 0;
}
Results:
How many strings u are going to enter?:
5
Enter Strings one by one:
I am a good guy
You are a bad guy
We are all good guy
Both OK
You can not go
Order of Sorted Strings:
Both OK
I am a good guy
We are all good guy
You are a bad guy
You can not go
READ  给出一个整数,然后用相反顺序打印出来 (递归函数, While 的应用)
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts