将字符串中的小写字母转换成大写字母C++版本
时间: 2024-05-04 21:20:51 浏览: 14
可以使用 C++ 标准库中的 `toupper()` 函数将小写字母转换成大写字母。以下是一个示例代码:
```c++
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string str = "Hello, World!";
for (char& c : str) {
if (std::islower(c)) {
c = std::toupper(c);
}
}
std::cout << str << std::endl;
return 0;
}
```
输出:
```
HELLO, WORLD!
```
在上面的代码中,`std::islower()` 函数用于检查字符是否为小写字母,`std::toupper()` 函数将小写字母转换成大写字母。使用一个循环遍历字符串中的每个字符,如果字符是小写字母,则将它转换成大写字母。
相关问题
将字符串中的小写字母转换成大写字母c++
可以使用 C++ 中的标准库函数 `toupper`,它可以将小写字母转换成大写字母。具体实现如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
for (int i = 0; i < str.size(); i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
}
cout << str << endl; // 输出 HELLO, WORLD!
return 0;
}
```
在上面的代码中,我们先判断字符串中每个字符是否为小写字母,如果是,则使用 `toupper` 将其转换为大写字母。最后输出转换后的字符串。
C++将字符串中的小写字母转换成大写字母
可以使用C++的标准库函数toupper()将字符串中的小写字母转换成大写字母,具体的实现如下:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string str = "Hello, World!";
int len = str.length();
for(int i=0; i<len; i++)
{
if(islower(str[i])) // 判断是否为小写字母
{
str[i] = toupper(str[i]); // 转换成大写字母
}
}
cout << str << endl;
return 0;
}
```
在上述代码中,我们首先定义了一个字符串变量str,并且获取了其长度len。然后利用for循环遍历字符串中的每一个字符,判断其是否为小写字母,如果是,则调用toupper()函数将其转换成大写字母。最后输出转换后的字符串即可。