原题链接:[编程入门]自定义函数之字符类型统计
解题思路:
1、可以使用gets()/scanf()获取待统计的字符。
2、scanf()获取字符的时候应该使用scanf("%[^\n]"),除了回车键全部读入。
注意事项:
参考代码:
#include<stdio.h>
#include<string.h>
void statistics(char *str, int *num_char, int *num_num, int *space_num, int *other_num)
{
int len=0, i=0;
len = strlen(str);
*num_char = 0;
*num_num = 0;
*space_num = 0;
*other_num = 0;
for(i = 0; i < len; i++)
{
if(str[i]>='0' && str[i]<='9')
*num_num+=1;
else if( (str[i]>='a' && str[i]<= 'z') || (str[i]>='A' && str[i]<='Z'))
*num_char+=1;
else if(str[i]==' ')
*space_num+=1;
else
*other_num+=1;
}
}
int main()
{
char Mystring[1024];
int num_char, num_num, space_num, other_num;
scanf("%[^\n]", Mystring);
statistics(Mystring, &num_char, &num_num, &space_num, &other_num);
printf("%d %d %d %d\n", num_char, num_num, space_num, other_num);
return 0;
}0.0分
29 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#include"stdio.h" #include"string.h" #include"ctype.h"//以便调用函数isalpha(),isdigit(),isblank() int tongji(char s[]) { int a=0,b=0,c=0,d=0,i,l; l=strlen(s); for(i=0;i<l;i++) { if(isalpha(s[i]))//判断是否为字母 a++; else if(isdigit(s[i]))//判断是否为数字 b++; else if(isblank(s[i]))//判断是否为空格 c++; else d++; } printf("%d %d %d %d\n",a,b,c,d); } int main() { char s[1000]; gets(s);//输入字符串 tongji(s); return 0; }#include<stdio.h> #include<ctype.h> int main() { int letter = 0,blank = 0,number = 0,others = 0,c; while((c = getchar()) != '\n') { if(isalpha(c) { letter ++; } else if(isdigit(c)) { number ++; } else if(c == ' ') { blank ++; } else { others ++; } } printf("%d %d %d %d\n",letter,number,blank,others); return 0; } 求问一下大佬 我这个为什么会超时呀//没人用ctype的库? 我贴一下 #include <stdio.h> #include <ctype.h> #include <string.h> void count(char str[]){ int i=0, digit=0, alpha=0, blank=0, others=0; while (str[i]!='\0'){ if(isdigit(str[i])){ digit++; } else if(isblank(str[i])){ blank++; } else if(isalpha(str[i])){ alpha++; }else { others++; } i++; } printf("%d %d %d %d\n", alpha, digit, blank, others); } int main(int argc, char* argv[]){ char str[100]; gets(str); count(str); return 0; }#include<stdio.h> #include<string.h> int count(char str[]){ int num=0,alpha=0,space=0,others=0; int sum=strlen(str); for(int i=0;i<sum;i++){ if(('A'<str[i]&&str[i]<'Z')||('a'<str[i]&str[i]<'z')){ alpha++; } else if('0'<str[i]&&str[i]<'9'){ num++; } else if(str[i]==' '){ space++; } else others++; } printf("%d %d %d %d",alpha,num,space,others); } int main(){ char a[1024]; scanf("%[^\n]",a); count(a); } 我devc出答案了 但是错误50 啥意思@晨续 #include<stdio.h> #include<string.h> int count(char str[]){ int num=0,alpha=0,space=0,others=0; int sum=strlen(str); for(int i=0;i<sum;i++){ if(('A'<str[i]&&str[i]<'Z')||('a'<str[i]&str[i]<'z')){ alpha++; } else if('0'<str[i]&&str[i]<'9'){ num++; } else if(str[i]==' '){ space++; } else others++; } printf("%d %d %d %d",alpha,num,space,others); } int main(){ char a[1024]; scanf("%[^ ]",a); count(a); }void statistics(char a[]); int main(void) { char a[1000]; gets(a); statistics(a); return 0; } void statistics(char a[]) { int letter,number,space,others=0; int i; for(i=0;a[i]!='\0';i++){ if((a[i]>='a' && a[i]<='z') || (a[i])>='A' && a[i]<='Z'){ letter++; }else if(a[i]>='0' && a[i]<='9'){ number++; }else if(a[i]==' '){ space++; }else{ others++; } } printf("%d %d %d %d",letter,number,space,others); } 为什么提交答案错误#include<stdio.h> #include<string.h> int tongji(char a[],int *c,int *n,int *b,int *o) { int i,k; i=strlen(a); for(k=0;k<i;k++) { if(a[k]>'a'&&a[k]<'z'||a[k]>'A'&&a[k]<'Z') (*c)++; else if(a[k]>='0'&&a[k]<='9') (*n)++; else if(a[k]==' ') (*b)++; else (*o)++; } } int main() { int c=0,n=0,b=0,o=0; char a[1000]; gets(a); tongji(a,&c,&n,&b,&o); printf("%d %d %d %d",c,n,b,o); return 0; } 为什么提交答案是错的#include<stdio.h> #include<string.h> void statistics(char *p,char *pf); int main(void) { int i; char str[100]={0}; char num[4]={0}; gets(str); statistics(str,num); for(i=0; i<4; i++) { printf("%d ",num[i]); } } void statistics(char *p,char *pf) { int i; for(i=0; p[i]!='\0'; i++) { if((p[i]>=65 && p[i]<=90) || (p[i]>=97 && p[i]<=122)) { pf[0]++; } else if(p[i]>=48 && p[i]<=57) { pf[1]++; } else if(p[i] == 32) { pf[2]++; } else { pf[3]++; } } }