题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
先拷贝一份来排序,再按顺序替换进去。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int compare(const void *a1, const void *b1)
{
char a=*(char *)a1;
char b=*(char *)b1;
if(a>='a' && a<='z')
a -= 32;
if(b>='a' && b<='z')
b -= 32;
if(a>='A'&&a<='Z' && b>='A'&&b<='Z' && abs(a-b) == 32)
a = b;
return a-b;
}
int main()
{
char str[1001];
char litter[1001];
fgets(str,sizeof(str),stdin);
strcpy(litter, str);
qsort(litter,strlen(litter),sizeof(char),compare);
for(int i=0, j=0; j<strlen(litter) && i<strlen(str);)
{
if(litter[j]>='A'&&litter[j]<='Z' || litter[j]>='a'&&litter[j]<='z')
{
if(str[i]>='A'&&str[i]<='Z' || str[i]>='a'&&str[i]<='z')
str[i++] = litter[j++];
else
i++;
}
else
j++;
}
printf("%s\n", str);
}
SHEIN希音公司福利 280人发布
查看9道真题和解析