零K沁雪


私信TA

用户名:qczl

访问量:74853

签 名:

零K沁雪

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

  自我简介:

解题思路:

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生成与解释

  评论区

这道题为什么要用指针啊???
2019-03-30 15:40:03
#include<stdio.h>
#include<string.h>
int * statistics(char *arr)
{  
   int len=strlen(arr);
   int letters=0;
   int numbers=0;
   int spaces=0;
   int others=0;
   for(int i=0;i<len;i++)
   if((arr[i]>='a' && arr[i]<='z')||(arr[i]>='A' && arr[i]<='Z'))
       letters++; 
    else if(arr[i]>='0'&&arr[i]<='9')
	   numbers++;
	else if(arr[i]==' ')//==是判断否等于,=是赋值;
		spaces++;
	 else   
         others++;
	 int ar[4]={letters,numbers,spaces,others};
	 return ar;
}
int main(void)
{
char arr[100];
gets(arr);
int *p=statistics(arr);
printf("%d %d %d %d\n",*p,*(p+1),*(p+2),*(p+3));
return 0;
} 为啥错误啊?
2019-02-19 15:53:09
scanf()获取字符的时候不使用scanf("%[^\n]"),好像也不会错吧
  求大佬求解
2018-12-28 15:23:24
各位看一下,我这个答案错误是为啥,谢了
#include <stdio.h>
#include <string.h>
void tj(char *a)    //定义函数
{
	int n=strlen(a); //求实际有效长度
	int i;
	int m=0,s=0,k=0,q=0;
	for(i=0;i<n;i++){
		if(a[i]==' ') k++;
		else if(a[i]>96&&a[i]<122) m++;
		else if(a[i]>64&&a[i]<91) m++;
		else if(a[i]>47&&a[i]<58) s++;
		else q++;
		a[i]=0;       //比较过后,将当前字符改为0;
	}
	a[0]=m+'0';
	a[1]=s+'0';
	a[2]=k+'0';       //将统计值改为字符类型
	a[3]=q+'0';       //并赋值给数组a的前4个元素,
}
int main()
{
	int i;
	char a[100]={0};
	gets(a);
	tj(a);
	for(i=0;i<5;i++){
	printf("%c ",a[i]);   //输出数组a的前4个元素
	}
	return 0;
}
2018-12-13 01:09:39
直接传地址,然后修改内存上的数据……这么简单粗暴的吗
2018-06-22 11:08:31
在实际使用中,直接在函数statistics中输出会有其他影响吗??
2018-05-30 23:22:35