解题思路:
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> #define STR_SIZE 1000 void flash(int* letter,int* number,int* space,int* other, char str1[STR_SIZE]) { fgets(str1,STR_SIZE,stdin); str1[strcspn(str1,"\n")]=0; int i; for(i=0;i<strlen(str1);i++) { if(str1[i]<='z'&&str1[i]>='a'||str1[i]<='Z'&&str1[i]>='A') { (*letter)+=1; } else if(str1[i]<='9'&&str1[i]>='0') { (*number)+=1; } else if(str1[i]==' ') { (*space)+=1; } else { (*other)+=1; } } printf("%d %d %d %d",*letter,*number,*space,*other); } int main() { int letter=0,number=0,space=0,other=0;
#include<stdio.h> #include<string.h> void draw(char a[]) { int len=strlen(a); int j=0,h=0,b=0,k=0; for(int i=0;i<len;i++) { if( (a[i]>='a' && a[i]<= 'z') || (a[i]>='A' && a[i]<='Z')) { j++; } else if(0<=a[i]<=9) { h++; } else if(a[i]==' ') { i++; } else { k++; } } printf("%d %d %d %d",j,h,b,k); } int main() { char a[200]; gets(a); draw(a); return 0; } 各位大佬哪里错了
滕 2023-05-22 11:23:42 |
1- a[i]>='0'&&a[i]<='9'(此处应使用单引号); 2-空格处为b++
22000901ym 2023-06-05 23:12:44 |
用了两个i在函数里
void PrintCount(char buff[]) { int NumberOfLetter = 0 ,NumberOfNum = 0 ,NumberOfSpace = 0 ,NumberOfOther = 0; int count = 0; while(buff[count] != '\0') { if(buff[count] >= '0' && buff[count] <= '9') { NumberOfNum++; } else if((buff[count] >= 'a' && buff[count] <= 'z') || (buff[count] >= 'A' && buff[count] <= 'Z')) { NumberOfLetter++; } else if(buff[count] == ' ') { NumberOfSpace++; } else { NumberOfOther++; } count++;
oula 2022-06-19 16:56:07 |
} printf("%d %d %d %d" ,NumberOfLetter ,NumberOfNum ,NumberOfSpace ,NumberOfOther); printf(" "); }
22000901ym 2023-06-05 23:12:29 |
用了两个i在函数里
#include<stdio.h> #include<string.h> void statisticsn(char a[],int l,int *words,int *maths,int *empty,int *other){ for(int i=0;i<l;i++){ if(a[i] >= 'A' && a[i] <= 'Z' || a[i] >= 'a' && a[i] <= 'z'){ (*words)++; } else if(a[i] >= '0' && a[i] <= '9'){ (*maths)++; } else if(a[i] == ' ') { (*empty)++; } else { (*other)++; } } } int main(){ char a[100]; gets(a); int length = strlen(a); int words=0,maths=0,empty=0,other=0; statisticsn(a,length,&words,&maths,&empty,&other); printf("%d %d %d %d",words,maths,empty,other); return 0; } 容易理解就完事了~~~
#include<stdio.h> #include<string.h> void fun(char *i,int j,int *a,int *b,int *c,int *d) { *a = 0; *b = 0; *c = 0; *d = 0; for(int m = 0;m < j;m++) { if((i[m] >= 'a' && i[m] <= 'z') || (i[m] >= 'A' && i[m] <= 'Z')) *a+=1; else if(i[m] >= '0' && i[m] <= '9') *b+=1; else if(i[m] == ' ') *c+=1; else *d+=1; } } int main() { char arr[100]; int l; int a,b,c,d; gets(arr); l = strlen(arr); fun(arr,l,&a,&b,&c,&d); printf("%d %d %d %d\n",a,b,c,d); return 0; }
#include<stdio.h> #include<string.h> int main() { int i,j=0,k=0,d=0,s=0,n; char a[100]; for(i=0;i<100;i++){ scanf("%c",&a[i]); } n=strlen(a); for(i=0;i<n;i++){ if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')) j++; else if(a[i]>='0'&&a[i]<='9') k++; else if(a[i]==' ') d++; else s++; } printf("%d %d %d %d",j,k,d,s); return 0; } 为什么错误
懒大王aq 2024-01-29 20:22:19 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void f(char*arr){ //int i,count1=0,count2=0,count3=0,count4=0; int i; int count1=0; int count2=0; int count3=0; int count4=0; for ( i = 0; i < strlen(arr); i++) { if (isalpha(arr[i])) { count1++; } else if (isdigit(arr[i])) { count2++; } else if (arr[i] == ' ') { count3++; } else{ count4++; } } count4--; printf("%d %d %d %d",count1,count2,count3,count4); } int main(int argc, char *argv[]) { char c[100]; //scanf("%s",c); //这里scanf就不对为什么啊?scanf忽略空格和换行 fgets(c,100,stdin); f(c); return 0; }为什么错
#include<stdio.h> #include<string.h> extern int q=0,w=0,e=0,r=0; void x(char a[30]) { int i,len; len=strlen(a); for(i=0;i<len;i++) { if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')) q++; else if(a[i]>='0'&&a[i]<='9') w++; else if(a[i]==' ') e++; else r++; } } int main() { char a[30]={}; gets(a); x(a); printf("%d %d %d %d",q,w,e,r); } 为什么结果正确但运行错误50呢?@.@
#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; }
矩阵转置 (C语言代码)浏览:1565 |
川哥的吩咐 (C语言代码)浏览:926 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:609 |
时间转换 (Java代码)浏览:617 |
矩形面积交 (Java代码)浏览:1281 |
弟弟的作业 (C++代码)浏览:1342 |
C语言训练-求函数值 (C语言代码)浏览:600 |
简单的a+b (C语言代码)浏览:674 |
WU-链表数据求和操作 (C++代码)浏览:1384 |
简单的a+b (C语言代码)浏览:574 |
菜旺 2024-02-19 11:05:38 |
#include<stdio.h> #include<string.h> #define STR_SIZE 1000 void flash(int* letter,int* number,int* space,int* other, char str1[STR_SIZE]) { fgets(str1,STR_SIZE,stdin); str1[strcspn(str1,"\n")]=0; int i; for(i=0;i<strlen(str1);i++) { if(str1[i]<='z'&&str1[i]>='a'||str1[i]<='Z'&&str1[i]>='A') { (*letter)+=1; } else if(str1[i]<='9'&&str1[i]>='0') { (*number)+=1; } else if(str1[i]==' ') { (*space)+=1; } else { (*other)+=1; } } printf("%d %d %d %d",*letter,*number,*space,*other); } int main() { int letter=0,number=0,space=0,other=0; char str1[STR_SIZE]; flash(&letter,&number,&space,&other,str1); return 0; }