原题链接:[编程入门]自定义函数之字符类型统计
解题思路:
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> int str(char a[],int *q,int *w,int *e,int *r) { int b,c,z = *q,x = *w,n = *e,v = *r; b = strlen(a); b--; for(c = b;c <= b;c --) { if(a[c] >= 97 && a[c] <= 90 || a[c] >= 97 && a[c] <= 122) z++; else if(a[c] >= 48 && a[c] <= 57) x++; else if(a[c] == 32) n++; else v++; if(c == 0) break; } *q = z; *w = x; *e = n; *r = v; return 0; } int main() { int q = 0,w = 0,e = 0,r = 0; char a[10000]; gets(a); str(a,&q,&w,&e,&r); printf("%d %d %d %d\n",q,w,e,r); return 0; } 这样为什么提示错误呢?请问这样为什么内存超限了呢? #include<stdio.h> #include<stdlib.h> int modify_statics(char *s,int length,int *num,int *c,int *space,int *other) { int i; for(i=0;i<length;i++) { if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122)) { *num+=1; }else if(s[i]>='0'&&s[i]<='9') { *c+=1; }else if(s[i]==' ') { *space+=1; }else { *other+=1; } } return 0; } int main() { int num=0,c=0,space=0,other=0,length; char *s; scanf("%[^\n]%n",s=(char*)calloc(length+1,sizeof(char)),&length); modify_statics(s,length,&num,&c,&space,&other); printf("%d %d %d %d\n",num,c,space,other); free(s); s=NULL; return 0; }#include<string.h> int JieGuo[4]; int *Tong_Ji(char *p, int Wei); int Shu[4] = { 0,0,0,0 }; int main() { char a[1000]; int *j; fgets(a, 1000, stdin); j = Tong_Ji(a, strlen(a)-1); for (int i = 0;i < 4;i++) { printf("%d ", *(j + i)); } } int *Tong_Ji(char *p, int Wei) { for (int i=0;i<Wei;i++) { if (*(p + i) > +'A'&&*(p + i) <= 'Z' || *(p + i) > +'a'&&*(p + i) <= 'z') Shu[0]++; else if (*(p + i) > +'0'&&*(p + i) <= '9') Shu[1]++; else if (*(p + i) == ' ') Shu[2]++; else Shu[3]++; } return Shu; } 为什么错误50% ? 哪位大神给解释下#include<stdio.h> #include<string.h> int * statistics(char *arr) { int len=strlen(arr); int letters=0; int numbers=0; int spaces=0; int others=0; for(int i=0;i<len;i++) if((arr[i]>='a' && arr[i]<='z')||(arr[i]>='A' && arr[i]<='Z')) letters++; else if(arr[i]>='0'&&arr[i]<='9') numbers++; else if(arr[i]==' ')//==是判断否等于,=是赋值; spaces++; else others++; int ar[4]={letters,numbers,spaces,others}; return ar; } int main(void) { char arr[100]; gets(arr); int *p=statistics(arr); printf("%d %d %d %d\n",*p,*(p+1),*(p+2),*(p+3)); return 0; } 为啥错误啊?scanf()获取字符的时候不使用scanf("%[^\n]"),好像也不会错吧 求大佬求解