帮我写一段c++ 函数 用于检查密码是否合法,密码要求:6位以上,字母和数字的组合。输入为一个string 返回true表示合法,false不合法
时间: 2024-05-06 07:19:29 浏览: 10
好的,我可以为您写一个C函数来检查密码是否合法。以下是代码示例:
```
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool isPasswordValid(char password[]) {
int length = strlen(password);
bool hasLetter = false, hasNumber = false;
if (length < 6) {
return false;
}
for (int i = 0; i < length; i++) {
if (isalpha(password[i])) {
hasLetter = true;
} else if (isdigit(password[i])) {
hasNumber = true;
}
}
return hasLetter && hasNumber;
}
```
这个函数接收一个字符串 `password` 作为输入,检查该密码是否符合要求。如果符合要求,返回 `true`,否则返回 `false`。
该函数通过将字符串长度与每个字符进行循环遍历,并检查字符是否为字母或数字来判断密码是否合法。如果输入的字符串长度小于6,则密码不合法。如果字符串中同时包含字母和数字,则认为密码合法。