提交记录 #490
提交时间:2024-12-10 17:00:35
语言:c
状态:Unaccepted
编译情况:编译成功
固定测试点#1:
额外测试点#1:
58【应用】元音排序
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// 判断字符是否为元音字母
int is_vowel(char c) {
c = tolower(c); // 统一转换为小写
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
// 对元音字符串进行排序
void sort_vowels(char *vowels, int length) {
// 先分离小写和大写字母
char lowercase[length + 1];
char uppercase[length + 1];
int lower_index = 0, upper_index = 0;
for (int i = 0; i < length; i++) {
if (islower(vowels[i])) {
lowercase[lower_index++] = vowels[i];
} else {
uppercase[upper_index++] = vowels[i];
}
}
lowercase[lower_index] = '\0'; // 添加字符串结束符
uppercase[upper_index] = '\0'; // 添加字符串结束符
// 对小写字母和大写字母分别进行排序
for (int i = 0; i < lower_index - 1; i++) {
for (int j = i + 1; j < lower_index; j++) {
if (lowercase[i] > lowercase[j]) {
char temp = lowercase[i];
lowercase[i] = lowercase[j];
lowercase[j] = temp;
}
}
}
for (int i = 0; i < upper_index - 1; i++) {
for (int j = i + 1; j < upper_index; j++) {
if (uppercase[i] > uppercase[j]) {
char temp = uppercase[i];
uppercase[i] = uppercase[j];
uppercase[j] = temp;
}
}
}
// 将小写字母和大写字母合并
strcpy(vowels, lowercase);
strcat(vowels, uppercase);
}
// 提取并排序元音字母
void extract_and_sort_vowels(const char *input, char *output) {
int index = 0;
// 遍历输入字符串,提取元音字母
for (int i = 0; input[i] != '\0'; i++) {
if (is_vowel(input[i])) {
output[index++] = input[i];
}
}
output[index] = '\0'; // 添加字符串结束符
// 对提取的元音字母进行排序
sort_vowels(output, index);
}
int main() {
const char *input = "Abort!May Be Some Errors In Out System. ";
char output[100]; // 用于存储排序后的元音字母
// 提取并排序元音字母
extract_and_sort_vowels(input, output);
printf("%s\n", output);
return 0;
}