咖啡


私信TA

用户名:Tianxn

访问量:128999

签 名:

十年OI一场空,不开LL见祖宗。

等  级
排  名 9
经  验 26168
参赛次数 10
文章发表 197
年  龄 22
在职情况 学生
学  校 西安电子科技大学
专  业 软件工程

  自我简介:

解题思路:C标准中有一个一个头文件<ctype.h>,这里面定义了一批C语言字符处理函数,用于测试字符是否属于特定的字符类别,如字母字符、控制字符、数字、等等;


头文件:<ctype.h>

函数:

    (1)int isalpha(int ch);

            判断ch是否为字母,如果是返回非0,反之返回0;

    

    (2)int isdigit(int ch);

            判断ch是否为数字,如果是返回非0,反之返回0;


    (3)int islower(int ch);

            判断ch是否为小写字母,如果是返回非0,反之返回0;

    

    (4)int isupper(int ch);

            判断ch是否为大写字母,如果是返回非0,反之返回0;

    

        ……

        等等很多处理字符的函数,大家有兴趣可以去看看博客。


注意事项:

参考代码:

#include <stdio.h>
#include <ctype.h>
int main()
{
    int letter = 0,number = 0,blank = 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;
}


 

0.0分

122 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

#include<stdio.h>
#include<string.h>
int main()
{
	char string[200];
	int i,y=0,s=0,k=0,q=0;
	char c;
	gets(string);
	c=string[i];
	for(i=0;i<strlen(string);i++)
	{
	if(c==' ')
	k++;
	else if('A'<=c&&c<='Z'||'a'<=c&&c<='z')
	{
		y++;
	}
	else if('0'<=c&&c<='9')
	{
		s++;
	}
	else
	{
		q++;
     	}
	}
 printf("%d %d %d %d ",y,s,k,q);
 return 0;
	
}
大佬,我的为什么不行呢,也不知道问题出哪了,可以帮看一下吗
2022-08-30 21:55:20
#include<stdio.h>
#include<math.h>
#include<ctype.h>
int main()
{
    char array[199];
    int alpha=0,digit=0,space=0,other=0;
    gets(array);
    int len=strlen(array);
    for(int i=0;i<len;i++){
        if isalpha(array[i]){
            alpha++;
        }else if isdigit(array[i]){
            digit++;
        }else if((int)array[i]==32){
            space++;
        }
    }
    other=len-alpha-digit-space;
    printf("%d %d %d %d",alpha,digit,space,other);
    return 0;
}
2022-03-02 13:43:36
//输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
//可以一个一个输入然后循环判断
#include<stdio.h>
int main()
{
    int english=0,number=0,bank=0,others=0;
    int a;
    while((a=getchar()) != '\n'){
        if (a>='a' && a<='z')
        {
            english++;
        }else if(a>='0' && a<='9')
        {
            number++;
        }else if(a==' ')
        {
            bank++;
        }else
        {
            others++;
        }
    }
    printf("%d %d %d %d",english,number,bank,others);
    return 0;
}
2022-02-07 15:45:32
求教,为什么答案不对。
#include<stdio.h>
#include<string.h>
int main()
{
    char st[200];
    int number=0,blank=0,other=0,leeter=0,i;
    gets(st);
    i=strlen(st);
    int n=0;
    for(n=0;n<i;n++)
    {
        if((st[n]<='z'&&st[n]>='a')||(st[n]>='A'&&st[n]<='Z'))
       leeter=leeter+1;
        else if(st[n]=' ')
        blank=blank+1;
        else if(st[n]<='9'&&st[n]>='1')
        number=number+1;
        else
        other=other+1;
        
    }
    printf("%d %d %d %d",number,other,blank,leeter);
    return 0;
    
}
2022-01-23 12:06:06
#include<stdio.h>
#include<string.h>
int main(){
	char a[100];
	int let=0,num=0,cnt=0,oth=0,n,i;
	gets(a);
	n=strlen(a);
	for(i=0;i<=n;i++){
	
	if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z'){
		let++;
	}else if(a[i]>='0'&&a[i]<='9'){
		num++;
	}else if(a[i]==' '){
		cnt++;
	}else{
		oth++;
	}
	}
	printf("%d %d %d %d\n",let,num,cnt,oth);
	
	
	
	return 0;
}  
大佬们,为什么我这么写其他字符会多一个啊(我吧other-1然后过了),是因为用了gets的缘故吗
2022-01-09 12:32:41
#include<stdio.h>
int main()
{
	char arr[200];
	int count = 0;
	int bit = 0;
	int cit = 0;
	int tit = 0;
	scanf("%[^\n]", arr);
	char* p = arr;
	while (*p !='\0')
	{
		if (*p == 32)
			count++;
		else if (*p > 64 && *p < 123)
			bit++;
		else if (*p > 47 && *p < 58)
			cit++;
		else
			tit++;
		*p++;
	}
	printf("%d %d %d %d", bit, cit, count, tit);
	return 0;
}
原来是用库函数解,亏我还上网查怎么输入空格。
2022-01-02 10:12:12
#include<stdio.h>
#include<string.h>
int main()
{
	char str[200],j;
	int i,a=0,b=0,c=0,d=0;
	gets(str);
	int len = strlen(str);
	for(i=0;i<len;i++)
	{
		j=str[i];
		if(j>='a'&&j<='z'||j>='A'&&j<='Z')
		{
			a++;
		}
		else if(j>='0'&&j<='9')
		{
			b++;
		}
		else if(j==' ')
		{
			c++;
		}
		else
		{
			d++;
		}
	}
	printf("%d %d %d %d",a,b,c,d);
}
2021-12-24 23:17:16
/*输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。*/
#include <stdio.h>
int main()
{
	char c;
	int zimu=0, shuzi=0, kongge = 0, qita = 0;
	while ((c = getchar()) != '\n')
	{
		if ('a' <= c && c<= 'z' || 'A' <= c && c <= 'Z')
		{
			zimu++;
		}
		else if ('0' <= c && c <='9')
			shuzi++;
		else if (c == ' ')
			kongge++;
		else
			qita++;
	}
	printf("%d %d %d %d\n", zimu, shuzi,kongge,qita);
}
2021-12-12 14:11:55