利用数组,输入3行英文,输出字母、空格及其他字符出现的个数。

利用数组,输入3行英文,输出字母、空格及其他字符出现的个数。用c++

用scanf就行。

#include <stdio.h>
void main()
{
int letter=0,space=0,other=0;
int row=0;
char c;
while(c=getchar())
{
if(c=='\n')
{
row++;
if(row==3)
break;
else
continue;
}
if(row==3)
break;
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
letter++;
else if(c==' ')
space++;
else
other++;
}
printf("letters number:%d\nspaces number:%d\nothers number:%d\n",letter,space,other);
}

 

#include <stdio.h>
void main() {
	int letter=0,space=0,other=0;
	int i;
	char s1[100],s2[100],s3[100];
	gets(s1);
	gets(s2);
	gets(s3);
	for(i=0; s1[i]; i++) {
		if(s1[i]>='a'&&s1[i]<='z'||s1[i]>='A'&&s1[i]<='Z')letter++;
		else if(s1[i]==' ')space++;
		else other++;
	}
	for(i=0; s2[i]; i++) {
		if(s1[i]>='a'&&s1[i]<='z'||s1[i]>='A'&&s1[i]<='Z')letter++;
		else if(s1[i]==' ')space++;
		else other++;
	}
	for(i=0; s3[i]; i++) {
		if(s1[i]>='a'&&s1[i]<='z'||s1[i]>='A'&&s1[i]<='Z')letter++;
		else if(s1[i]==' ')space++;
		else other++;
	}
	printf("字母个数是%d.\n空格个数是%d.\n其他个数是%d.\n",letter,space,other);
}

代码如上,万望采纳

代码如下,如有帮助,请采纳一下,谢谢。

#include <stdio.h>
#include <string>
int main()
{
	char buf[3][1024] = {0};
	for (int i = 0; i < 3; i++)
	{
		gets(buf[i]);
	}

	int nmbzm = 0;  //字母数量
	int nmbkg = 0;  //空格数量
	int nmbqt = 0;  //其它字符数量
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < strlen(buf[i]);j++)
		{
			if ( (buf[i][j] >='a' && buf[i][j] <= 'z') || (buf[i][j] >='A' && buf[i][j] <= 'Z'))
			{
				nmbzm++;
			}else if (buf[i][j] == ' ')
			{
				nmbkg++;
			}else
				nmbqt++;
		}
	}
	printf("字母数量:%d;空格数量:%d;其它字符数量:%d\n",nmbzm,nmbkg,nmbqt);

	//getchar();
	//getchar();
	return 0;
}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632