解题思路:
按照ASCII码表对应的数进行编写,统计各个数字出现的次数。
注意事项:
scanf见到空格会吞掉数组,所以使用gets来进行字符串的存储。
参考代码:
其中<bits/stdc++.h>(万能头文件)可以修改为<iostream><cstring>两个头文件
#include<bits/stdc++.h> #define max 1000 using namespace std; int main() { int a=0,b=0,c=0,d=0; char arr[max]; gets(arr); int len=strlen(arr); for(int i=0; i<len; i++) { if((arr[i]>='A'&&arr[i]<='Z')||(arr[i]>='a'&&arr[i]<='z')) a++; else if(arr[i]>='0'&&arr[i]<='9') b++; else if(arr[i]==32) c++; else d++; } cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl; return 0; }
0.0分
17 人评分
#include<stdio.h> #include<string.h> int fw(char a[]){ int b=0,c=0,d=0,e=0; for(int i=0;i<strlen(a);i++){ if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')) b++; else if(a[i]>='0'&&a[i]<='9') c++; else if(a[i]==' ') d++; else e++; } printf("%d %d %d %d",b,c,d,e); } int main() {char a[50]; gets(a); fw(a); } 这个为什么百分之五十???
sorry,这是函数修正版的 #include <cstdio> #include <cstring> #include <cctype> void Number(char str[]) { int alpha=0,digit=0,space=0,other=0; for(int i=0; i<strlen(str); i++) { if(isalpha(str[i])) ++alpha; else if(isdigit(str[i])) ++digit; else if(str[i]==32) ++space; else ++other; } printf("%d %d %d %d",alpha,digit,space,other); } int main() { char str[100]; gets(str); Number(str); return 0; }
方法很好,但是没有满足题目要求,题目要求要写函数传递参数,其他同学可以参考下哈 #include <cstdio> #include <iostream> #include <cstring> #include <cctype> using namespace std; void Number(char str[]) { int alpha=0,digit=0,space=0,other=0; for(int i=0; i<strlen(str); i++) { if(isalpha(str[i])) ++alpha; else if(isdigit(str[i])) ++digit; else if(str[i]==32) ++space; else ++other; } cout<<alpha<<" "<<digit<<" "<<space<<" "<<other<<endl; printf("%d %d %d %d",alpha,digit,space,other); } int main() { char str[100]; gets(str); Number(str); return 0; }
全网第一白给Jhin 2020-04-13 19:57:24 |
大佬,我想问一下,gets(str)是什么意思?有啥作用?秋梨膏!!先谢谢了。
今天我Code了吗 2023-01-10 16:02:38 |
跟cin一样 但可以计算空格
红颜如霜 2023-09-27 07:57:07 |
@JhinJax 输入字符串
红颜如霜 2023-09-27 07:57:31 |
@JhinJax 输入字符串
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:592 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:1007 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:682 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:597 |
Pascal三角 (C语言代码)浏览:1180 |
简单的a+b (C语言代码)浏览:523 |
母牛的故事 (C语言代码)浏览:548 |
矩阵加法 (C语言代码)浏览:1715 |
关于float,double变量的几点说明浏览:1810 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:397 |