编写程序,输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。(while)

编写程序,输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。(while)

#include<stdio.h>
int main(void)
{
char c;
int letter=0, number=0, space=0, other=0;
printf("请输入一行字符:  \n");
while((c=getchar())!='\n') 
{
	if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
		letter++;
	else if(c>='0'&&c<='9')
		number++;
	else if(c==' ')
		space++;
	else
		other++;
}
printf("字母%d个  数字%d个 空格%d个   其他%d个\n",letter,number,space,other);
return 0;
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用以下 Python 代码来实现这个程序: ```python def count_characters(): text = [] try: while True: line = input() text.append(line) except EOFError: pass # 将多行文本合并为单个字符串 text = '\n'.join(text) # 统计空格数 space_count = text.count(' ') # 统计大小写字母数 lowercase_count = sum(1 for char in text if char.islower()) uppercase_count = sum(1 for char in text if char.isupper()) # 统计其他字符数 other_count = len(text) - space_count - lowercase_count - uppercase_count return space_count, lowercase_count, uppercase_count, other_count space_count, lowercase_count, uppercase_count, other_count = count_characters() print(f"空格数:{space_count}") print(f"大小写字母数:{lowercase_count + uppercase_count}") print(f"小写字母数:{lowercase_count}") print(f"大写字母数:{uppercase_count}") print(f"其他字符数:{other_count}") ``` 这个程序使用了一个 `count_characters` 函数来接收用户输入的多行文本,并统计空格数、大小写字母数和其他字符数。在函数中,我们使用了 `input` 函数来逐行接收用户输入的文本,直到用户输入完成(通过按下 Ctrl+D 或 Ctrl+Z)。 然后,我们将多行文本合并为单个字符串,并使用字符串的 `count` 方法来统计空格数。我们还使用了生成器表达式和字符的 `islower` 和 `isupper` 方法来统计大小写字母数。 最后,我们计算其他字符数,即总字符数减去空格数、大小写字母数。最后,我们将结果打印来。 请注意,在运行程序时,请使用 Ctrl+D(Unix / Linux / macOS)或 Ctrl+Z(Windows)来标记输入完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简而言之希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值