题解 | #字符串排序###感觉是一个非常弱智的写法##浪费内存效率低#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include<stdio.h>
#include<string.h>
#define N 1000
#include<stdbool.h>
bool judgesym(char c) {
if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')
return false;
else
return true;
}
typedef struct symbol
{
char c;
int pos;
}sym;
int main()
{
char str1[N];
char str[N];
char *result = (char*)malloc(sizeof(char)*strlen(str1)+1);
sym s[N];
gets(str1);
int k = 0, l = 0;
for (int i = 0; i < strlen(str1); i++)
{
if (judgesym(str1[i])) //如果是符号
{
s[l].c = str1[i];
s[l].pos = i;
l++;
}
else //如果是字母
str[k++] = str1[i];
}
str[k] = '\0';
for (int i = 0; i < k; i++)
for (int j = 0; j < k - i - 1; j++)
{
if ((str[j] >= 'A'&&str[j] <= 'Z'&&str[j + 1] >= 'A'&&str[j + 1] <= 'Z' && str[j] > str[j + 1]) || (str[j] >= 'a' && str[j] <= 'z'&& str[j + 1] >= 'a'&&str[j + 1] <= 'z' && str[j] > str[j + 1]))
{
char temp = str[j + 1];
str[j + 1] = str[j];
str[j] = temp;
}
else if (str[j] >= 'A'&&str[j] <= 'Z'&&str[j + 1] >= 'a'&&str[j + 1] <= 'z' && (str[j] > str[j + 1] - 32))
{
char temp = str[j + 1];
str[j + 1] = str[j];
str[j] = temp;
}
else if (str[j] >= 'a'&&str[j] <= 'z'&&str[j + 1] >= 'A'&&str[j + 1] <= 'Z' && (str[j] - 32 > str[j + 1]))
{
char temp = str[j + 1];
str[j + 1] = str[j];
str[j] = temp;
}
else
continue;
}
int temp = 0;
int i =0;
for( i=0; i<strlen(str1); i++)
{
for(int j=0;j<l;j++)
{
if(s[j].pos == i)
{
result[i++] = s[j].c;
}
}
result[i] = str[temp++];
}
result[i]='\0';
puts(result);
return 0;
}