#include <cs50.h>
#include <stdio.h>
#include <string.h>
bool isSymbol(string symbol);
bool isUpper(string front);
bool isLower(string front);
bool isNumber(string number);
bool isPassword(string sentence);
int main(void)
{
string sentence = get_string("Enter your password:\n");
if(isPassword(sentence))
{
printf("Your password is valid!\n");
}else
{
printf("Your password needs at least one uppercase letter, lowercase letter, number and symbol!\n");
}
}
//大小写字母
bool isUpper(string front)
{
for(int i = 0, n = strlen(front);i < n;i++)
{
if(front[i] >= 'A' && front[i] <= 'Z')
{
return true;
}
}
return false;
}
bool isLower(string front)
{
for(int i = 0, n = strlen(front);i < n;i++)
{
if(front[i] >= 'a' && front[i] <= 'z')
{
return true;
}
}
return false;
}
//特殊字符有33~47,58~64,91~96,123~126,创建一个函数
bool isSymbol(string symbol)
{
//获取symbol获取长度,需要string.h的strlen
for(int i = 0, n = strlen(symbol); i < n; i++)
{
if((symbol[i] >= 33 && symbol[i] <= 47) || (symbol[i] >= 58 && symbol[i] <= 64 ) || (symbol[i] >= 91 && symbol[i] <= 96 ) || (symbol[i] >= 123 && symbol[i] <= 126 ))
{
return true;
}
}
return false;
}
bool isNumber(string number)
{
for(int i = 0, n = strlen(number);i < n;i++)
{
if(number[i] >= '1' && number[i] <= '9')
{
return true;
}
}
return false;
}
//创建一系列判断变量,如果所有的结果为true,则密码可用
bool isPassword(string sentence)
{
if(isSymbol(sentence) && isUpper(sentence) && isLower(sentence) && isNumber(sentence))
{
return true;
}
return false;
}
按照提示可以分别创建函数来判断,利用cs课堂提供的思路遍历字符串。