标签:upper inpu strong 轻松 none for 工具 \n stat
问题描述:
请分别编制程序,完成下面的处理:
(1)统计字母‘A‘出现的次数;
(2)统计字符串中(大/小写)字母个数;
(3)统计每一个元音字母个数
(4)统计每一个数字字符出现的次数;
提示:可以定义一个数组int a[10],保存’0’-’9’在字符串中出现的次数(例a[0]表示字符’0’的个数)
(5)统计每一个字母出现的个数
#include "stdafx.h" #include <string.h> #include <ctype.h> int main() { char str[99]; printf("Please input the string:"); gets_s(str,99); int i(0), a_num(0); while (str[i]) { if (str[i] == ‘A‘ ) { a_num++; } i++; } printf("number of letter A is %d.\n", a_num); i = 0; int upper_num(0), lower_num(0); while (str[i]) { if (iswlower(str[i])) { lower_num++; } if (iswupper(str[i])) { upper_num++; } i++; } printf("number of lowercase letter is %d, uppercase is %d.\n", lower_num, upper_num); i = 0; int vowel_num[5] = {0,0,0,0,0}; while (str[i]) { switch (str[i]) { case ‘a‘:vowel_num[0]++; break; case ‘e‘:vowel_num[1]++; break; case ‘i‘:vowel_num[2]++; break; case ‘o‘:vowel_num[3]++; break; case ‘u‘:vowel_num[4]++; break; default:break; } i++; } printf("number of vowel letters:\n"); printf("\ta\te\ti\to\tu\n"); for (int i = 0; i < 5; i++) { printf("\t%d", vowel_num[i]); } printf("\n"); i = 0; static int digit_num[10]; for (int i = 0; str[i]; i++) { if (isdigit(str[i])) { digit_num[str[i]-‘0‘]++; } } printf("number of digits:\n"); for (int i = 0; i < 10; i++) { printf("\tnumber%d", i); } printf("\n"); for (int i = 0; i < 10; i++) { printf("\t%d", digit_num[i]); } printf("\n"); i = 0; static int alpha_num[26]; for (int i = 0; str[i]; i++) { if (isalpha(str[i])) { if (isupper(str[i])) { str[i] += ‘a‘ - ‘A‘; } alpha_num[str[i] - ‘a‘]++; } } printf("number of each letter:\n"); for (int i = 0;i<27; i++) { if (alpha_num[i] !=0) { printf("%c = %d\n", ‘a‘+i, alpha_num[i]); } } printf("others are none."); printf("\n"); return 0; }
感想:
活用这种工具网站可以轻松查询到函数及其用法
http://www.runoob.com/
=
标签:upper inpu strong 轻松 none for 工具 \n stat
原文地址:https://www.cnblogs.com/miyazakehime/p/9348250.html