任意读入一个字符串,统计其中大写字母、小写字母、数字字符出现的次数。 例如,读入“The PRC was founded on October 1st,1949.” 则输出: 字符串The PRC was founded on October 1st,1949.中含有: 5个大写字母 22个小写字母 5个数字字符 #include <iostream> #include <___________> using namespace std; int main() {char s[80],*p; int U,L,D; ______________________; gets(s); for(_________;*p!='\0'; ___________) if(isupper(*p)) U++; else if(islower(*p)) L++; else if(___________) D++; cout<<"字符串"<<_______<<"中含有:"<<endl; cout<<U<<"个大写字母"<<endl; cout<<L<<"个小写字母"<<endl; cout<<D<<"个数字字符"<<endl; return 0; }
时间: 2023-12-06 18:41:28 浏览: 29
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char s[80],*p;
int U=0,L=0,D=0;
cin.getline(s, 80); // 使用 getline 读入字符串,避免 gets 的安全问题
for(p=s;*p!='\0';p++) {
if(isupper(*p))
U++;
else if(islower(*p))
L++;
else if(isdigit(*p))
D++;
}
cout<<"字符串"<<s<<"中含有:"<<endl;
cout<<U<<"个大写字母"<<endl;
cout<<L<<"个小写字母"<<endl;
cout<<D<<"个数字字符"<<endl;
return 0;
}
相关问题
任意读入一个字符串,统计其中大写字母、小写字母、数字字符出现的次数。 例如,读入“The PRC was founded on October 1st,1949.” 则输出: 字符串The PRC was founded on October 1st,1949.中含有: 5个大写字母 22个小写字母 5个数字字符
好的,我明白了你的要求。以下是我的回答:
可以通过循环遍历字符串中的每个字符,并使用 Python 内置的 isupper()、islower() 和 isdigit() 方法判断字符是否为大写字母、小写字母和数字字符,然后分别累加计数即可。
示例代码如下:
```
s = input('请输入一个字符串:')
upper_count = 0
lower_count = 0
digit_count = 0
for c in s:
if c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
elif c.isdigit():
digit_count += 1
print('字符串{}中含有:{}个大写字母{}个小写字母{}个数字字符'.format(s, upper_count, lower_count, digit_count))
```
例如,输入字符串“ThePRCwasfoundedonOctober1st,1949.”,则输出:
```
字符串ThePRCwasfoundedonOctober1st,1949.中含有:5个大写字母22个小写字母5个数字字符
```
C++编程,任意读入一个字符串,统计其中大写字母、小写字母、数字字符出现的次数。 例如,读入“The PRC was founded on October 1st,1949.” 则输出: 字符串The PRC was founded on October 1st,1949.中含有: 5个大写字母 22个小写字母 5个数字字符
好的,我能够回答您关于C编程的问题。
统计字符串中大写字母、小写字母、数字字符出现的次数的C代码如下:
```
#include <stdio.h>
int main()
{
char str[100];
int i, upper_count=0, lower_count=0, digit_count=0;
printf("请输入字符串:\n");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='A' && str[i]<='Z')
upper_count++;
else if(str[i]>='a' && str[i]<='z')
lower_count++;
else if(str[i]>='0' && str[i]<='9')
digit_count++;
}
printf("字符串%s中含有:%d个大写字母,%d个小写字母,%d个数字字符\n", str, upper_count, lower_count, digit_count);
return 0;
}
```
在这个程序中,我们定义了一个字符串变量str,以及三个计数变量upper_count、lower_count和digit_count,分别用于统计大写字母、小写字母、数字字符的出现次数。然后,使用gets函数从用户处获取输入的字符串,并通过for循环遍历字符串中的每一个字符,根据ASCII码判断该字符是大写字母、小写字母还是数字字符,并将对应的计数器加1。最后,输出统计结果。
希望这个程序能够帮到您。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)