编程题:计算字符串中的元音、辅音、数字、空白符的数量
输入一个字符串: runoob123 erkcomsww dfrunoob 元音: 8 辅音: 15 数字: 3 空白符: 2
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include <windows.h> #define N 50 int main(void) { int i=0, vowels, consonants, digits, spaces;//元音 辅音 数字 空格 vowels = consonants = digits = spaces = 0; char string[N],word; puts("请输入字符串"); gets(string); int n = strlen(string); string[n] = '\0'; while (string[i] != '\0') { if (string[i] == 'a'|| string[i] == 'A'|| string[i] == 'E' || string[i] == 'e' || string[i] == 'i' || string[i] == 'I' || string[i] == 'o' || string[i] == 'U' || string[i] == 'u' || string[i] == 'U' ) { vowels++; } else if ((string[i]>='a'&& string[i] <= 'z')||(string[i] >= 'A' && string[i] <= 'Z')) {//除了元音全是辅音 consonants++; } else if (string[i] >= '0' && string[i] <= '9') { digits++; } else if(string[i] == ' ') { spaces++; } i++; } printf("元音个数:%d\n辅音个数:%d\n数字个数:%d\n空格个数:%d\n", vowels, consonants, digits, spaces);
return 0; }
#include<iostream>
using namespace std;
int main()
{
char source[1024];
char * string=source;
gets(source);
cout<<string<<endl;
int vowel=0;
int consonant=0;
int digit=0;
int space=0;
while(*string)
if(*string=='a' || *string=='A' || *string=='e' || *string=='E' || *string=='o' || *string=='O' || *string=='i' || *string=='I' || *string=='u' || *string=='U')
vowel++;
}else if(*string>='0' && *string<='9')
digit++;
}else if(*string==' ')
space++;
}else
consonant++;
}
string++;
cout<<"vowel:\t"<<vowel<<endl;
cout<<"consonant:\t"<<consonant<<endl;
cout<<"digit:\t"<<digit<<endl;
cout<<"space:\t"<<space<<endl;
return 0;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> int main() { char line[150]; int i, vowels, consonants, digits, spaces; vowels = consonants = digits = spaces = 0; printf("输入一个字符串: "); scanf("%[^\n]", line); for(i=0; line[i]!='\0'; ++i) { if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') { ++vowels; } else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) { ++consonants; } else if(line[i]>='0' && line[i]<='9') { ++digits; } else if (line[i]==' ') { ++spaces; } } printf("元音: %d",vowels); printf("\n辅音: %d",consonants); printf("\n数字: %d",digits); printf("\n空白符: %d", spaces); return 0; }
登录后提交答案