题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[1000] = "";
gets(str);
int len = strlen(str);
int ch[1000] = {0};
int k = 0;
//把A-Z 依次存到ch
for(int i = 0 ;i < 26;i++)//每趟收集一个字符到ch数组(A-Z)
{
for(int j = 0;j < len;j++)
{
if(toupper(str[j]) == 'A'+i)
{
ch[k++] = str[j];
}
}
}
//遍历,改变原数组顺序,打印输出
k = 0;
for(int i = 0;i < len;i++)
{
if(isalpha(str[i]))
str[i] = ch[k++];
}
printf("%s\n",str);
return 0;
}
#华为机试题#