解题思路:
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分
39 人评分
#include<stdio.h> #include<string.h> int fun(char a[100]) { int n,i,len,cnt1=0,cnt2=0,cnt3=0,cnt4=0; len=strlen(a); for(i=0;i<len;i++) { if(a[i]>='a'&&'z'>=a[i]||a[i]>='A'&&a[i]<='Z') cnt1++; else if(a[i]>='0'&&a[i]<='9') cnt2++; else if(a[i]==' ') cnt3++; else cnt4++; } printf("%d %d %d %d",cnt1,cnt2,cnt3,cnt4); } int main() { int n,i; char b[100]; gets(b); fun(b); return 0; } 就这样不简单得多吗?
#include <stdio.h> void func(char *str, int len, int *a, int *b, int *c, int *d) { for (int i=0; i<len; i++) { if (str[i]>='a' && str[i]<='z') { (*a)++; } else if (str[i]>='A' && str[i]<='Z') { (*a)++; } else if (str[i]>='0' && str[i]<='9') { (*b)++; } else if (str[i] == ' ') { (*c)++; } else { (*d)++; } } } int main(void) { char str[256]; int a=0, b=0, c=0, d=0; gets(str); func(str, 256, &a, &b, &c, &d); printf("%d %d %d %d\n", a, b, c, d); return 0; } 哪里错了?
seahorizon 2020-07-29 15:15:42 |
把func(str, 256, &a, &b, &c, &d);改成func(str, strlen(str), &a, &b, &c, &d);
seahorizon 2020-07-29 15:16:11 |
再加上头文件string.h
#include<stdio.h> int main() { char s[100]; int a=0,b=0,c=0,d=0,i; gets(s); for(i=0;i<100;i++) { if(s[i]=='\0') break; if(s[i]<=90&&s[i]>=65) a++; else if(s[i]<=122&&s[i]>=97) a++; else if(s[i]<57&&s[i]>48) b++; else if(s[i]==' ') c++; else d++; } printf("%d %d %d %d",a,b,c,d); return 0; } 求问哪里出错了50%
景明 2020-07-07 10:27:57 |
请看清题目,题目说要求使用函数
#include <stdio.h> int main() { void add(char n); char ch; scanf("%c",&ch); add(ch); } void add(char n) { int a=0,b=0,c=0,d=0; while((n=getchar())!='\n') { if((n>='a'&&n<='z')||(n>='A'&&n<='Z')) {a++;} else if(n>='0'&&n<='9') {b++;} else if(n==' ') {c++;} else {d++;} } printf("%d %d %d %d",a,b,c,d); } 时间超限。。。
#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% ? 哪位大神给解释下
奔跑吧C 2019-05-11 08:07:09 |
汗!!自己把>= 给写成> +了
林致远 2020-09-08 20:35:55 |
墨染 2020-10-16 15:35:05 |
题目是在主函数中输出