问题描述:
写一函数,将一个字符串中的元音字母复制到另一个字符串,然后输出。
样例输入:
abcde
样例输出:
ae
问题分析:
我们定义了一个名为 copyVowels 的函数。这个函数接受两个参数:input 是指向输入字符串的指针,output 是指向存放元音字母的字符串的指针。函数通过遍历输入字符串,如果遇到元音字母('a', 'e', 'i', 'o', 'u'),就将其复制到输出字符串中,最后在输出字符串的末尾添加一个空字符 \0 以表示字符串结束。
代码实现:
#include <stdio.h>
void copyVowels(const char *input, char *output) {
while (*input != '\0') {
char c = *input;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
*output = c;
output++;
}
input++;
}
*output = '\0'; // 在目标字符串末尾添加空字符,以表示字符串的结束
}
int main() {
char input[100];
char output[100];
scanf("%s", input); // 使用scanf读取输入的字符串
copyVowels(input, output);
printf("%s", output);
return 0;
}