寻光


私信TA

用户名:RITD

访问量:22435

签 名:

追寻最优解

等  级
排  名 545
经  验 4284
参赛次数 0
文章发表 13
年  龄 20
在职情况 学生
学  校 East Electricity
专  业 计科

  自我简介:

很菜所以很努力

解题思路: 此题可用两种方法来做

方法一:

1.了解ASCII表,那些字符在哪里,用那些字符对应的数字,也可不用了解,直接用字符就行

2.要声明4个变量分别记录题目(字母、数字、空格、其他字符)要求的数量,和一个字符变量利用getchar()获得赋值

3.用getchar()利用while循环读取字符,while循环里用if else嵌套给对应的字符数加一

 4.利用printf()输出出来

方法二:

如果对gets(),strlen(),数组不了解的话,此法可不看

1.先声明一个数组用于gets()来存储输入

声明4个变量来存储对应的字符数

2.strlen()获取输入的长度,用于后面的for循环

3.利用for循环计算各字符数

20200728090157350.png
注意事项: 注意while循环里里面的条件

参考代码:

1.利用getchar()

#include <stdio.h>
int
main(void)
{
	int letter = 0, number = 0, space = 0, other = 0;
	char ch;
	
	while( (ch = getchar()) != '\n'){     /*这里的‘\n’不能换成EOF,要不就把输入的换行符输入进来当成其他字符处理,
	                                        因此其他字符的数量将比题目所给的数量多一*/
	    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
	    	letter += 1;   //letter表示字母数量
	    else
		if (ch == ' ')
	    	    space += 1;  //空格数量
	    	else
		    if (ch >= '0' && ch <= '9')
	    		number += 1; //数字数量
	    	    else
	    		other += 1;  //其他字符数量
	}
	printf("%d %d %d %d", letter, number, space, other);
	
	return 0;
}

ii:for(),数组处理

#include <stdio.h>
#include <string.h> 
int
main(void)
{
	char str[200];//声明一个数组用于存储输入的字符
	int letter = 0, number = 0, space = 0, other, n;
	
	gets(str); //获取输入并存储到数组里,gets会把输入的换行符\n丢弃
	n = strlen (str); //获取输入的字符长度
	
	
	
	for(int i = 0; i < n; i++)//for循环可以声明变量同时初始化,多个声明用 , 逗号隔开
	{
		if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
			letter += 1;
		if (str[i] == ' ')
		    space += 1;
		if (str[i] >= '0' && str[i] <= '9')
			number += 1;		 
	}
	other = n - space - number - letter; //把整个字符长度减去字母数、空格、数字得出其他字符的数量
	
	printf("%d %d %d %d", letter, number, space, other);
	
	return 0;
}


 

0.0分

84 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

哪一个更好啊
2024-04-19 22:03:41
#include<stdio.h>
#include<string.h>
int i,al,num,k,q;
int main(){
	char a[250];
	int len=0;
	/*scanf("%[^\n]%*c",a);
	len=strlen(a);
	printf("%d\n",len);*/
	fgets(a,sizeof(a),stdin);
	len=strlen(a)-1;
	//printf("%d\n",len);
	for(i=0;i<len;i++){
		if(a[i]>='a'&&a[i]<'z'||a[i]>='A'&&a[i]<='Z') al++;
		else if(a[i]>='0'&&a[i]<='9') num++;
		else if(a[i]==' ') k++;
		else q++;
	}
	printf("%d %d %d %d",al,num,k,q);
	return 0;
}为什么第二个测试点一直过不去啊
2024-04-08 23:35:11
#include<stdio.h>
int main()
{
    int letter=0,number=0,space=0,other=0;
    char ch;
    while ((ch=getchar())!='n'){
        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
            letter+=1;
        }
        else if(ch>='0'&&ch<='9'){
            number+=1;
        }
        else if(ch==' '){
            space+=1;
        }
        else{
            other+=1;
        }
    }
    printf("%d %d %d %d",letter,number,space,other);
    return 0;
}
大佬们请问我这个哪里错了啊
2024-03-31 17:44:29
#include<stdio.h>
int main()
{
    int n;
    long long x=1,Sn=0;
    scanf("%d",&n);
    if(n==0){
        Sn=0;
    }else{
        for(int i=1;i<=n;i++){
            x=x*i;
            Sn=Sn+x;
        }
    }
    printf("%lld",Sn);
    return 0;
}
2024-03-03 17:44:21
#include <stdio.h>

int main(void){
    int letter = 0,number = 0,space = 0,other = 0;
    char ch;
    while( (ch = getchar()) !='\n'){
        if((ch >='A'&& ch <='Z')||(ch >='a'&&ch <='z'))
            letter += 1;
        else if(ch ==' ')
            space += 1;
        else if(ch>='0'&& ch<='9')
            number += 1;
        else
            other += 1;
            printf("%d %d %d %d", letter, number, space, other);
    }
    
    return 0;
}
//这是哪里出问题了,显示答案错误
2024-02-05 11:09:03
#include<stdio.h>
#include<string.h>
int main()
{
  char str[100];
  int letter=0,number=0,kongge=0,other=0;
  int n;
  gets(str);
  n=strlen(str);
  for(int i=0;i<=n;i++)
  {
      if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')
        letter+=1;
      if(str[i]==' ')
        kongge+=1;
      if(str[i]>='0'&&str[i]<='9')
        number+=1;
        
  }
  other=n-letter-kongge-number;
  printf("%d %d %d %d",letter,kongge,number,other);
  return 0;
}为什么提交时一直显示答案错误呢?
但是在线测试里面是算得出正确结果的啊?
哪位大神可以解释一下哇!
2024-01-19 12:58:58
#include<stdio.h>
int main() {
	char c;
	int c1=0,c2=0,c3=0,c4=0;
	printf("输入一行字符\n");
	while((c=getchar())!='\n') {

		if(c>='A'&&c<='Z'||c>='a'&&c<='z')
			c1++;
		else if(c>='0'&&c<='9')
			c2++;
		else if(c==' ')
			c3++;
		else
			c4++;
	}
	printf("%d%d%d%d",c1,c2,c3,c4);
	return 0;
}
各位大神,我这哪里错了
2023-11-25 12:32:53
\n,这个是你打完字符时打个回车然后就结束循环开始运行
2023-10-23 10:51:47