编程题:计算字符串中的元音、辅音、数字、空白符的数量
输入一个字符串: runoob123 erkcomsww dfrunoob 元音: 8 辅音: 15 数字: 3 空白符: 2
//没考虑大写,改一下判断就行了 #include <bits/stdc++.h> using namespace std; int main() { vector<char> arr1; char c1; // 读取所有字符(包括空格,直到换行符) while ((c1 = cin.get()) != '\n') { arr1.push_back(c1); } int yuan = 0; int fu = 0; int num = 0; int kong = 0; for(int i = 0; i < arr1.size();i++){ if(arr1[i] == 'a' ||arr1[i] == 'e' ||arr1[i] == 'i' ||arr1[i] == 'o' ||arr1[i] == 'u' ){ yuan++; } else if((arr1[i] >= 'a' && arr1[i] <= 'z')&& (arr1[i] != 'a' &&arr1[i] != 'e'&&arr1[i] != 'i' &&arr1[i] != 'o' &&arr1[i] != 'u')){ fu++; }else if(arr1[i] >= '0' && arr1[i] <= '9'){ num++; } else if(arr1[i] == ' '){ kong++; } } cout << yuan << endl; cout << fu << endl; cout << num << endl; cout << kong << endl; }
1
#include <stdio.h>
#define a 100
void main()
{
char ch[a];
scanf("%s",ch);
int blank_s=num_s=y_e=f_e=0;
for(int i=0; i<strlrn(ch) ;i++)
if(ch[i]==' ') blank_s++;
if(ch[i])=='1'||ch[i])=='2'||ch[i])=='3') num_s++;
if(ch[i])=='a'||ch[i])=='e'||ch[i])=='i'||ch[i])=='o'||ch[i])=='u') y_e++;
f_e=strlen(ch)-blank_s-num_s-y_e;
}
printf("元音:%d\n",&y_s);
printf("辅音:%d\n",&f_s);
printf("数字:%d\n",&num_s);
printf("空白符:%d",&blank_s);
#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; }
登录后提交答案