题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
思路
- 得到输入数据
- 将字符串中的字符不分大小写按顺序存到新数组
- 输出结果,原字符串中为大小写字母的用新字符串替换
Answer
#include<stdio.h>
#include<string.h>
int main() {
char str[1001] = {0};
int index = 0;
gets(str);
int len = strlen(str);
char str_ch[len];
for (char j = 'A'; j <= 'Z'; j++) {
for (int i = 0; i < len; i++) {
if ((str[i] == j)
|| ( (str[i] - 32 == j)
&& str[i] >= 'a' && str[i] <= 'z')) {
str_ch[index++] = str[i];
}
}
}
index = 0;
for (int i = 0; i < len; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
str[i] = str_ch[index++];
}
printf("%c", str[i]);
}
return 0;
}