原题链接:[编程入门]自定义函数之字符类型统计
解题思路:
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> #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; }#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;@uq_87534612122 #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> 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; } 各位大佬哪里错了@oula } printf("%d %d %d %d" ,NumberOfLetter ,NumberOfNum ,NumberOfSpace ,NumberOfOther); printf(" "); }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++;#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; } 容易理解就完事了~~~