c++对于给定字符如果是大小写字母或者数字就输出YES否则输出NO
时间: 2024-06-13 22:07:51 浏览: 17
以下是C++判断字符是否为大小写字母或数字的代码,如果是则输出YES,否则输出NO:
```c++
#include <iostream>
using namespace std;
int main() {
char a;
a = getchar();
if ((a >= '0' && a <= '9') || (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
```
相关问题
c++大小写字母转换字符串
以下是C++中大小写字母转换字符串的例子:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Hello World!";
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
} else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = tolower(str[i]); // 将大写字母转换为小写字母
}
}
cout << str << endl; // 输出:hELLO wORLD!
return 0;
}
```
c++ 输入一个字符串,判断是否有八位,是否存在大小写字母和数字,存在输出yes
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len, has_digit = 0, has_upper = 0, has_lower = 0, has_eight = 0;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
if (len == 8)
has_eight = 1;
for (int i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9')
has_digit = 1;
else if (str[i] >= 'A' && str[i] <= 'Z')
has_upper = 1;
else if (str[i] >= 'a' && str[i] <= 'z')
has_lower = 1;
}
if (has_eight && has_digit && has_upper && has_lower)
printf("yes");
else
printf("no");
return 0;
}
相关推荐
![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)