输入字符串按字符顺序升序输出

题目描述:
输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。


输入:
测试数据有多组,输入字符串。


输出:
对于每组输入,输出处理后的结果。


样例输入:
bacd
样例输出:

abcd


c语言代码:

#include <stdio.h>
#include <string.h>
int main()
{
	char s[200];
	char t;
	int i,j;
	gets(s);
	for(i=0;i<strlen(s);i++)
		for(j=i;j<strlen(s);j++)
		{
			if(s[j]<s[i])
			{
				t=s[j];
				s[j]=s[i];
				s[i]=t;
			}
		}
	puts(s);
	return 0;
}


加入while循环,需要加入memset函数清除之前的s

#include<stdio.h>
#include<string.h>
#include<memory.h>
int main()
{   
	char s[200];
	char t; 
	int i,j;
	while(gets(s)!=NULL)
        {
            for(i=0;i<strlen(s)-1;i++)
            	for(j=i;j<strlen(s);j++)
                {
                	if(s[j]<s[i])
                  	{
                    	t=s[j];
                    	s[j]=s[i];
                    	s[i]=t;
                  	}
              	}
        puts(s);
        memset(s,0,sizeof(s));
        }
}


程序运行截图:




python代码,我们依旧强调简明易懂:

s = input("input string: ")
l = list(s)
l.sort()
s = "".join(l)
print(s)


猜你喜欢

转载自blog.csdn.net/weixin_41656968/article/details/80270433