使用遍历,将整个字符串判断


字符串的文字个数不能太多,否则容易时间超标

需要引用string.h进行字符个数判断

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int a = 0,b = 0,c = 0,d = 0;//a表示字母的个数,b表示空格的个数,c表示数字的个数,d表示其他字符的个数
	int i;
	char x[1000000];
	gets(x);
	for(i = 0;i < strlen(x);i++)
	{
		if(x[i] <= 'z' && x[i] >='a' || x[i] <= 'Z' && x[i] >='A')//要考虑到大写和小写的区别
		{
			a++;
		}else
		if(x[i] == ' ')
		{
			b++;
		}else
		if(x[i] >= '0' && x[i] <='9')
		{
			c++;
		}else{
			d++;
		}
	}
	printf("%d\n%d\n%d\n%d\n",a,b,c,d);//最后输出格式输出
}


 

0.0分

2 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

c++编译器 gets(x);会报错
2022-04-29 21:33:20
  • «
  • 1
  • »