C++小写字母转换为大写字母程序
时间: 2024-06-12 14:10:05 浏览: 15
以下是C++小写字母转换为大写字母的程序:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[101];
cin.getline(str, 101); // 读入字符串
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') { // 判断是否为小写字母
str[i] -= 32; // 转换为大写字母
}
}
cout << str << endl; // 输出转换后的字符串
return 0;
}
相关问题
C++小写字母转换为大写字母
C++中可以使用两种方法将小写字母转换为大写字母。第一种方法是利用ASCII码,将小写字母的ASCII码减去32,得到对应的大写字母的ASCII码,然后再将其转换为字符输出。第二种方法是使用STL库中的函数toupper(char s),直接将小写字母转换为大写字母。两种方法都可以实现小写字母转换为大写字母的功能。
用c++编写小写字母转换为大写字母
以下是一个简单的 C++ 代码示例,用于将输入的小写字母转换为大写字母:
```cpp
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char c;
cout << "Enter a lowercase letter: ";
cin >> c;
if (islower(c)) {
c = toupper(c);
cout << "The uppercase letter is: " << c << endl;
} else {
cout << "You did not enter a lowercase letter." << endl;
}
return 0;
}
```
该程序首先提示用户输入一个小写字母,然后使用 `islower()` 函数检查用户输入的字符是否为小写字母。如果是,使用 `toupper()` 函数将其转换为大写字母,并输出结果。否则,程序将提示用户重新输入。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)