题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
/*******************
*合理利用循环遍历(存在已知要求)+character[index++]
********************/
#include <stdio.h>
#include <string.h>
int main() {
char input[1001] = {0};
char character[1001] = {0};
int index = 0;
while( fgets(input,1001,stdin) != NULL )
{
//双重循环遍历26个字母,通过index指针完成字符串记录循环
for( char j = 'A'; j<='Z'; j++ )
{
for( int i = 0; i<strlen(input); i++ )
{
if(input[i] == j || ( (input[i] - 32 == j) && input[i] >= 'a'
&& input[i] <= 'z' ))
{
character[index++] = input[i];
}
}
}
//循环遍历输入数组,判断其中是字母的进行替换,不是进行保留(单循环+index指针)
index = 0;
for( int i = 0; i<strlen(input); i++ )
{
if( (input[i] >= 'a' && input[i] <= 'z')
|| (input[i] >= 'A' && input[i] <= 'Z' ))
{
input[i] = character[index++];
}
printf( "%c", input[i] );
}
}
return 0;
}
