c++判断字母大小写
时间: 2023-06-29 07:12:49 浏览: 115
在C++中,可以使用标准库函数`isupper()`和`islower()`来判断一个字符是否为大写或小写字母。这两个函数都需要传入一个字符参数,返回值为布尔类型,表示是否为大写或小写字母。示例代码如下:
```c++
#include <iostream>
#include <cctype>
int main() {
char ch = 'A';
if (isupper(ch)) {
std::cout << ch << " is an uppercase letter." << std::endl;
} else {
std::cout << ch << " is not an uppercase letter." << std::endl;
}
ch = 'b';
if (islower(ch)) {
std::cout << ch << " is a lowercase letter." << std::endl;
} else {
std::cout << ch << " is not a lowercase letter." << std::endl;
}
return 0;
}
```
输出结果为:
```
A is an uppercase letter.
b is a lowercase letter.
```
相关问题
c++大小写字母
在C++中,我们可以使用`toupper()`和`tolower()`函数来实现字母的大小写转换。这两个函数都定义在头文件`<cctype>`中。
`toupper()`函数将小写字母转换为大写字母,`tolower()`函数将大写字母转换为小写字母。
下面是一个示例代码:
```c++
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char c = 'A';
if(isupper(c))
{
c = tolower(c);
cout << "转换为小写字母:" << c << endl;
}
else if(islower(c))
{
c = toupper(c);
cout << "转换为大写字母:" << c << endl;
}
else
{
cout << "不是字母!" << endl;
}
return 0;
}
```
输出结果为:
```
转换为小写字母:a
```
在这个例子中,我们首先使用`isupper()`函数判断字符是否为大写字母。如果是,就使用`tolower()`函数将其转换为小写字母,并输出转换后的结果。如果不是大写字母,就使用`islower()`函数判断是否为小写字母,如果是,就使用`toupper()`函数将其转换为大写字母,并输出转换后的结果。如果既不是大写字母也不是小写字母,就输出“不是字母!”。
希望这可以帮助你!
符串字母大小写转换c++
以下是将字符串中的字母大小写进行转换的C程序:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Original string: %s", str);
for (i = 0; str[i]; i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
} else if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}
printf("Modified string: %s", str);
return 0;
}
```
代码解析:
1. 使用 `fgets` 函数读取用户输入的字符串,存储在 `str` 数组中。
2. 遍历字符串中的每个字符,使用 `islower` 函数判断是否为小写字母,使用 `toupper` 函数将小写字母转换为大写字母;使用 `isupper` 函数判断是否为大写字母,使用 `tolower` 函数将大写字母转换为小写字母。
3. 输出转换后的字符串。
注意事项:
1. 由于 `fgets` 函数会将用户输入的换行符也包含在字符串中,因此在遍历字符串时需要判断字符串是否结束。
2. 由于 `toupper` 和 `tolower` 函数需要包含 `ctype.h` 头文件,因此需要添加该头文件。