BUPT-homework/semester1/pset12/1-3-INCORRECT-GetVowel.c

37 lines
1,004 B
C
Raw Normal View History

// https://bbc3502.hopto.org/c2021/index.php/home-17/590-3
2021-12-05 10:54:29 +08:00
/* ⚠*** This code is not perfect *** */
#include <stdio.h>
//将只包含小写字母的字符串str中的元音字母复制到字符数组vowel,并返回元音字符的个数。
int getVowel(char str[], char vowel[]);
int main() {
char vowel[101], str[101]; //每个数组都至少要101个字节
int len;
scanf("%s", str); //读入字符串
len = getVowel(str, vowel); //复制
if (len > 0)
printf("%d %s\n", len, vowel); //输出复制后结果
else
printf("%d\n", len); //仅输出长度
return 0;
}
/* 请在这里填写答案 */
#include <string.h>
#pragma GCC optimize("Ofast,inline")
int getVowel(char str[], char vowel[]) {
char vowelstr[] = {'a', 'e', 'i', 'o', 'u', '\0'};
2021-12-05 10:54:29 +08:00
int len = 0;
for (int i = 0, n = strlen(str); i < n; i++) {
for (int j = 0, m = strlen(vowelstr); j < m; j++) {
2021-12-05 10:54:29 +08:00
if (vowelstr[j] == str[i]) {
vowel[len++] = str[i];
}
}
}
return len;
}