读入一些字符串,将其中的小写字母转成大写字母(其他字符不变)。c++
时间: 2024-03-03 13:52:52 浏览: 27
可以使用 `toupper()` 函数将小写字母转成大写字母,代码如下:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
int main() {
const int MAXN = 100;
char str[MAXN];
std::cout << "Please enter a string: ";
std::cin.getline(str, MAXN);
for (int i = 0; i < strlen(str); i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
}
std::cout << "The result is: " << str << std::endl;
return 0;
}
```
运行结果:
```
Please enter a string: Hello, world!
The result is: HELLO, WORLD!
```
相关问题
编写一个函数,将字符串中的小写字母转换为大写字母,主函数输入字符串并输出结
您好!以下是将字符串中的小写字母转换为大写字母的函数,您可以将其加入到您的程序中:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void toUpper(char *str) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] -= 32;
}
}
}
int main() {
char str[100];
cout << "请输入字符串:" << endl;
cin.getline(str, 100);
toUpper(str);
cout << "转换后的字符串为:" << endl;
cout << str << endl;
return 0;
}
```
在该程序中,toUpper() 函数用来将字符串中的小写字母转换为大写字母。该函数的参数为 char 类型的指针,表示要转换的字符串。函数中,我们使用了 for 循环遍历字符串中的每一个字符,判断并转换小写字母。其中,小写字母的 ASCII 码范围为 97-122,大写字母的 ASCII 码范围为 65-90,它们的 ASCII 码差值为 32,因此将小写字母的 ASCII 码减去 32 就可以得到相应的大写字母的 ASCII 码。最后,我们在主函数中读入字符串,调用 toUpper() 函数进行转换,并输出转换后的字符串。
希望能够帮到您!
将字符串中的小写字母转换成大写字母 题目描述 给定一个字符串,将其中所有的小写字母转换成大写字母。 输入格式 输入一行,包含一个字符串(长度不超过100,可能包含空格)。 输出格式 输出转换后的字符串。使用c++
可以使用C语言中的标准库函数`toupper`来将小写字母转换成大写字母。具体方法如下:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[101];
fgets(str, 101, stdin); // 读入字符串
for (int i = 0; str[i] != '\0'; i++)
{
if (islower(str[i])) // 判断是否为小写字母
printf("%c", toupper(str[i])); // 转换成大写字母并输出
else
printf("%c", str[i]); // 否则直接输出
}
return 0;
}
```
其中,`islower`函数用于判断是否为小写字母,`toupper`函数用于将小写字母转换成大写字母。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)