1216 - C语言8.19
写一个函数,将一个字符串中的元音字母复制到另一个字符串,然后在主函数中输出。
Input
只有一行,有一个包含空格的字符串。保证字符串的长度不超过100。
Output
将读入的字符串中的元音字母复制出的字符串。
请注意行尾输出换行。
Examples
Input
THIS is not a program.
Output
Iioaoa
Solution C
#include<stdio.h> #include<string.h> int main() { char a[204],i,l; gets(a); l=strlen(a); for(i=0;i<l;i++) { if(a[i]=='a'||a[i]=='A') printf("%c",a[i]); if(a[i]=='o'||a[i]=='O') printf("%c",a[i]); if(a[i]=='e'||a[i]=='E') printf("%c",a[i]); if(a[i]=='i'||a[i]=='I') printf("%c",a[i]); if(a[i]=='u'||a[i]=='U') printf("%c",a[i]); } printf("\n"); return 0; }
Solution C++
#include <stdio.h> #include <string.h> int main() { void pick(char str[], int l, char out[]); char str[101], out[101]; gets(str); pick(str, strlen(str), out); puts(out); return 0; } char tolower(char a) { if ('A' <= a && a <= 'Z') return 'a' + (a - 'A'); return a; } void pick(char str[], int l, char out[]) { int i, lout = 0; for (i = 0;i < l;i++) if (tolower(str[i]) == 'a' || tolower(str[i]) == 'e' || tolower(str[i]) == 'i' || tolower(str[i]) == 'o' || tolower(str[i]) == 'u') { out[lout++] = str[i]; } out[lout] = '\0'; }