【题目描述】 给定一个字符串,将其中所有的小写字母转换成大写字母。 【输入】 输入一行,包含一个字符串(长度不超过100,可能包含空格)。 【输出】 输出转换后的字符串。 【输入样例】 helloworld123Ha 【输出样例】 HELLOWORLD123HA C++代码
时间: 2023-12-03 16:05:51 浏览: 58
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[105];
cin.getline(str, 105);
int len = strlen(str);
for(int i = 0; i < len; i++) {
if(str[i] >= 'a' && str[i] <= 'z') { // 判断是否为小写字母
str[i] = str[i] - 'a' + 'A'; // 转换成大写字母
}
}
cout << str << endl;
return 0;
}
```
相关问题
给定一个字符串,将其中所有的小写字母转换成大写字母。C++
可以使用 C++ 中的 toupper 函数将小写字母转换成大写字母,具体实现如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[] = "Hello, World!";
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z') { // 如果是小写字母
s[i] = toupper(s[i]); // 转换成大写字母
}
}
cout << s << endl; // 输出转换后的字符串
return 0;
}
```
输出结果为:
```
HELLO, WORLD!
```
给定一个字符串,将其中所有的小写字母转换成大写字母。
可以使用字符串的upper()方法将所有小写字母转换成大写字母,示例代码如下:
```python
s = "Hello, World!"
s = s.upper()
print(s) # 输出 "HELLO, WORLD!"
```
其中,upper()方法会返回一个新的字符串,原字符串不会被修改。