C++程式檢查密碼強度
假設我們有一個字串S,S是一個密碼。如果密碼滿足以下所有條件,則密碼複雜:
密碼長度至少為5個字元;
密碼至少包含一個大寫字母;
密碼至少包含一個小寫字母;
密碼至少包含一個數字。
我們需要檢查密碼S的質量。
問題類別
為了解決這個問題,我們需要操作字串。程式語言中的字串是儲存在特定陣列式資料型別中的字元流。幾種語言將字串指定為特定資料型別(例如Java、C++、Python);而其他幾種語言則將字串指定為字元陣列(例如C)。字串在程式設計中非常重要,因為它們通常是各種應用程式中首選的資料型別,並用作輸入和輸出的資料型別。有各種字串操作,例如字串搜尋、子串生成、字串剝離操作、字串轉換操作、字串替換操作、字串反轉操作等等。檢視下面的連結,瞭解如何在C/C++中使用字串。
https://tutorialspoint.tw/cplusplus/cpp_strings.htm
https://tutorialspoint.tw/cprogramming/c_strings.htm
因此,如果我們問題的輸入類似於S = "NicePass52",則輸出將為Strong。
步驟
為了解決這個問題,我們將遵循以下步驟:
a := false, b := false, c := false, d := false if size of s >= 5, then: a := true for initialize i := 0, when i < call length() of s, update (increase i by 1), do: if s[i] >= '0' and s[i] <= '9', then: b := true if s[i] >= 'A' and s[i] <= 'Z', then: c := true if s[i] >= 'a' and s[i] <= 'z', then: d := true if a, b, c and d all are true, then: return "Strong" Otherwise return "Weak"
示例
讓我們看看下面的實現,以便更好地理解:
#include <bits/stdc++.h>
using namespace std;
string solve(string s){
bool a = false, b = false, c = false, d = false;
if (s.length() >= 5)
a = true;
for (int i = 0; i < s.length(); i++){
if (s[i] >= '0' && s[i] <= '9')
b = true;
if (s[i] >= 'A' && s[i] <= 'Z')
c = true;
if (s[i] >= 'a' && s[i] <= 'z')
d = true;
}
if (a && b && c && d)
return "Strong";
else
return "Weak";
}
int main(){
string S = "NicePass52";
cout << solve(S) << endl;
}輸入
"NicePass52"
輸出
Strong
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP