零K沁雪


私信TA

用户名:qczl

访问量:74854

签 名:

零K沁雪

等  级
排  名 40
经  验 12041
参赛次数 3
文章发表 35
年  龄 0
在职情况 在职
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

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分

36 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区

#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;
	
}
就这样不简单得多吗?
2020-07-26 11:07:47
函数声明应该要加上吧
2020-07-19 11:16:28
#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;
}
哪里错了?
2020-06-02 22:02:03
#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-03-09 18:49:23
#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);
}

时间超限。。。
2020-02-16 16:32:43
#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;
}
这样为什么提示错误呢?
2019-08-13 12:56:54
请问这样为什么内存超限了呢?
#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;
}
2019-08-05 12:19:57
#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%  ?  哪位大神给解释下
2019-05-10 12:34:34