编写一个c++程序,从键盘输入的一组字符中统计出大写字母的个数m和小写字母的个数n,并输出m、n中的较大者
时间: 2024-01-27 08:05:54 浏览: 30
以下是一个统计大写字母和小写字母个数并输出较大者的C++程序:
```c++
#include <iostream>
using namespace std;
int main() {
char c;
int m = 0, n = 0;
while (cin >> c) {
if (isupper(c)) { // 判断是否为大写字母
m++;
} else if (islower(c)) { // 判断是否为小写字母
n++;
}
}
int max_num = (m > n) ? m : n; // 求较大者
cout << max_num << endl;
return 0;
}
```
程序中使用了 `isupper()` 和 `islower()` 函数来判断输入的字符是否为大写字母和小写字母,并使用三元运算符 `?:` 来求出较大者。注意,程序中使用了 `iostream` 头文件来读取键盘输入。
相关问题
运用c++编写一个程序,输入一个字符串,然后计算并输出该字符串中大写字母和小写字母的个数
当使用C++编写一个程序来计算字符串中大写字母和小写字母的个数时,可以按照以下步骤进行:
1. 引入必要的头文件:
```cpp
#include <iostream>
#include <string>
```
2. 定义主函数:
```cpp
int main() {
// 代码将在这里编写
return 0;
}
```
3. 获取用户输入的字符串:
```cpp
std::string input;
std::cout << "请输入一个字符串:";
std::getline(std::cin, input);
```
4. 声明并初始化大写字母和小写字母的计数变量:
```cpp
int uppercaseCount = 0;
int lowercaseCount = 0;
```
5. 遍历字符串中的每个字符,并判断其是否为大写字母或小写字母:
```cpp
for (char c : input) {
if (std::isupper(c)) {
uppercaseCount++;
} else if (std::islower(c)) {
lowercaseCount++;
}
}
```
6. 输出结果:
```cpp
std::cout << "大写字母个数:" << uppercaseCount << std::endl;
std::cout << "小写字母个数:" << lowercaseCount << std::endl;
```
完整代码如下所示:
```cpp
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "请输入一个字符串:";
std::getline(std::cin, input);
int uppercaseCount = 0;
int lowercaseCount = 0;
for (char c : input) {
if (std::isupper(c)) {
uppercaseCount++;
} else if (std::islower(c)) {
lowercaseCount++;
}
}
std::cout << "大写字母个数:" << uppercaseCount << std::endl;
std::cout << "小写字母个数:" << lowercaseCount << std::endl;
return 0;
}
```
c++编写程序.能够输入一行不含空格的字符串.统计其中的大写字母小写字母和数字的个数
好的,以下是C++编写的程序,可以实现输入一行不含空格的字符串,统计其中的大写字母、小写字母和数字的个数:
```c++
#include <iostream>
using namespace std;
int main() {
string str;
int upper = 0, lower = 0, digit = 0;
cout << "请输入一行不含空格的字符串:" << endl;
getline(cin, str); // 输入一行字符串
for (char c : str) {
if (c >= 'A' && c <= 'Z') {
upper++;
} else if (c >= 'a' && c <= 'z') {
lower++;
} else if (c >= '0' && c <= '9') {
digit++;
}
}
cout << "大写字母个数:" << upper << endl;
cout << "小写字母个数:" << lower << endl;
cout << "数字个数:" << digit << endl;
return 0;
}
```
程序思路如下:
1. 使用 `string` 类型的变量 `str` 存储输入的字符串;
2. 定义三个变量 `upper`、`lower` 和 `digit` 分别代表大写字母、小写字母和数字的个数,初始化为 0;
3. 使用 `getline` 函数从标准输入流中读取一行字符串赋值给 `str`;
4. 使用 `for` 循环遍历字符串中的每个字符,通过字符的 ASCII 码值判断它是大写字母、小写字母还是数字,并分别对应将 `upper`、`lower` 和 `digit` 变量的值加 1;
5. 输出统计结果。
运行程序,输入一行不含空格的字符串,即可得到大写字母、小写字母和数字的个数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)