{"id":512019,"date":"2024-01-09T00:27:00","date_gmt":"2024-01-08T16:27:00","guid":{"rendered":"https:\/\/deepinout.com\/?p=512019"},"modified":"2024-01-04T21:42:51","modified_gmt":"2024-01-04T13:42:51","slug":"1696346586_j_password-validation-in-cpp","status":"publish","type":"post","link":"https:\/\/deepinout.com\/cpp\/cpp-top-tutorials\/1696346586_j_password-validation-in-cpp.html","title":{"rendered":"C++ \u8fdb\u884c\u5bc6\u7801\u9a8c\u8bc1"},"content":{"rendered":"<h1>C++ \u8fdb\u884c\u5bc6\u7801\u9a8c\u8bc1<\/h1>\n<p>\u5bc6\u7801\u9a8c\u8bc1\u662f\u7f51\u7edc\u5b89\u5168\u4e2d\u4e00\u4e2a\u7ecf\u5e38\u88ab\u5ffd\u89c6\u7684\u91cd\u8981\u65b9\u9762\u3002\u5bc6\u7801\u662f\u9632\u6b62\u672a\u77e5\u8bbf\u95ee\u8d26\u6237\u6216\u7cfb\u7edf\u7684\u7b2c\u4e00\u9053\u9632\u7ebf\uff0c\u786e\u4fdd\u5176\u5f3a\u5ea6\u53ef\u4ee5\u9632\u6b62\u8bb8\u591a\u7f51\u7edc\u653b\u51fb\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8<a href=\"https:\/\/deepinout.com\/c-language-tutorial\/c-top-tutorials\/1695773895_j_c-programming-language-tutorial.html\" data-internallinksmanager029f6b8e52c=\"18\" title=\"C\u8bed\u8a00\u6559\u7a0b\" target=\"_blank\" rel=\"noopener\">C<\/a>++\u4e2d\u7684\u5bc6\u7801\u9a8c\u8bc1\uff0c\u5e76\u4ecb\u7ecd\u5b9e\u73b0\u5bc6\u7801\u9a8c\u8bc1\u7684\u5404\u79cd\u6280\u672f\u548c\u65b9\u6cd5\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u5bc6\u7801\u9a8c\u8bc1<\/h3>\n<p>\u5bc6\u7801\u9a8c\u8bc1\u662f\u68c0\u67e5\u7528\u6237\u8f93\u5165\u5bc6\u7801\u7684\u5f3a\u5ea6\u548c\u5b89\u5168\u6027\u7684\u8fc7\u7a0b\u3002\u9a8c\u8bc1\u8fc7\u7a0b\u6d89\u53ca\u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u7b26\u5408\u67d0\u4e9b\u6807\u51c6\uff0c\u5982\u957f\u5ea6\u3001\u590d\u6742\u6027\u548c\u552f\u4e00\u6027\u3002\u5bc6\u7801\u9a8c\u8bc1\u5bf9\u4e8e\u9632\u6b62\u672a\u7ecf\u6388\u6743\u8bbf\u95ee\u8d26\u6237\u548c\u7cfb\u7edf\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h3>\u5bc6\u7801\u9a8c\u8bc1\u6280\u672f<\/h3>\n<p>\u6709\u5404\u79cd\u5bc6\u7801\u9a8c\u8bc1\u6280\u672f\u53ef\u7528\u4e8e\u786e\u4fdd\u5bc6\u7801\u7684\u5f3a\u5ea6\u548c\u5b89\u5168\u6027\u3002\u4e00\u4e9b\u5e38\u7528\u7684\u6280\u672f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u957f\u5ea6\u68c0\u67e5\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u8be5\u6280\u672f\u6d89\u53ca\u68c0\u67e5\u5bc6\u7801\u7684\u957f\u5ea6\u3002\u5177\u6709\u6700\u5c0f\u957f\u5ea6\u8981\u6c42\u7684\u5bc6\u7801\u6bd4\u8f83\u77ed\u7684\u5bc6\u7801\u66f4\u5b89\u5168\u3002<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6027\u68c0\u67e5\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u8be5\u6280\u672f\u6d89\u53ca\u68c0\u67e5\u5bc6\u7801\u7684\u590d\u6742\u6027\u3002\u5305\u542b\u5927\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\u7684\u5bc6\u7801\u88ab\u8ba4\u4e3a\u66f4\u590d\u6742\u548c\u5b89\u5168\u3002<\/p>\n<ul>\n<li><strong>\u5b57\u5178\u68c0\u67e5\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u8be5\u6280\u672f\u6d89\u53ca\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u662f\u8bcd\u5178\u4e2d\u7684\u5355\u8bcd\u6216\u5e38\u7528\u5bc6\u7801\u3002\u8fd9\u6837\u7684\u5bc6\u7801\u5bb9\u6613\u88ab\u731c\u6d4b\uff0c\u53ef\u80fd\u4f1a\u5371\u53ca\u8d26\u6237\u6216\u7cfb\u7edf\u7684\u5b89\u5168\u3002<\/p>\n<ul>\n<li><strong>\u5386\u53f2\u68c0\u67e5\uff1a<\/strong><\/li>\n<\/ul>\n<p>\u8be5\u6280\u672f\u6d89\u53ca\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u66fe\u7ecf\u4f7f\u7528\u8fc7\u3002\u4e4b\u524d\u4f7f\u7528\u8fc7\u7684\u5bc6\u7801\u5bb9\u6613\u88ab\u731c\u6d4b\uff0c\u56e0\u6b64\u4e0d\u5b89\u5168\u3002<\/p>\n<h3>C++\u4e2d\u7684\u5bc6\u7801\u9a8c\u8bc1<\/h3>\n<p><a href=\"https:\/\/deepinout.com\/c-language-tutorial\/c-top-tutorials\/1695773895_j_c-programming-language-tutorial.html\" data-internallinksmanager029f6b8e52c=\"18\" title=\"C\u8bed\u8a00\u6559\u7a0b\" target=\"_blank\" rel=\"noopener\">C<\/a>++\u4e2d\u7684\u5bc6\u7801\u9a8c\u8bc1\u6d89\u53ca\u5b9e\u73b0\u4e0a\u8ff0\u4e00\u4e2a\u6216\u591a\u4e2a\u5bc6\u7801\u9a8c\u8bc1\u6280\u672f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5728C++\u4e2d\u5b9e\u73b0\u5bc6\u7801\u9a8c\u8bc1\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5728C++\u4e2d\u8fdb\u884c\u957f\u5ea6\u68c0\u67e5\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684length()\u51fd\u6570\u6765\u5b9e\u73b0C++\u4e2d\u7684\u957f\u5ea6\u68c0\u67e5\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4ee5\u4e0b\u662f\u5728C++\u4e2d\u5b9e\u73b0\u957f\u5ea6\u68c0\u67e5\u7684\u793a\u4f8b\uff1a<\/p>\n<p><strong>C++\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-cpp line-numbers\">#include \n#include \nusing namespace std;\n\nint main() {\n    string password;\n    cout &lt;&lt; \"Enter password: \";\n    getline(cin, password);\n    if (password.length() &lt; 8) {\n        cout &lt;&lt; \"Password is too short.\" &lt;&lt; endl;\n    }\n    else {\n        cout &lt;&lt; \"Password is strong.\" &lt;&lt; endl;\n    }\n    return 0;\n}\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre><code class=\"language-cpp line-numbers\">Enter password: abdh\nPassword is too short.\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5bc6\u7801\uff0c\u5e76\u4f7f\u7528 <strong>length()<\/strong> \u51fd\u6570\u6765\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u81f3\u5c11\u67098\u4e2a\u5b57\u7b26\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>\u5728C++\u4e2d\u8fdb\u884c\u590d\u6742\u5ea6\u68c0\u67e5\uff1a<\/strong><\/p>\n<p>\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>isdigit()\uff0cislower()\uff0cisupper()<\/strong> \u548c <strong>ispunct()<\/strong> \u51fd\u6570\u6765\u5b9e\u73b0\u590d\u6742\u5ea6\u68c0\u67e5\u3002\u8fd9\u4e9b\u51fd\u6570\u5206\u522b\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u3001\u5c0f\u5199\u5b57\u6bcd\u3001\u5927\u5199\u5b57\u6bcd\u6216\u6807\u70b9\u7b26\u53f7\u3002\u4e0b\u9762\u662f\u5728C++\u4e2d\u5b9e\u73b0\u590d\u6742\u5ea6\u68c0\u67e5\u7684\u793a\u4f8b\uff1a<\/p>\n<p><strong>C++\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-cpp line-numbers\">#include \n#include \nusing namespace std;\n\nint main() {\n    string password;\n    bool has_upper = false, has_lower = false, has_digit = false, has_punct = false;\n    cout &lt;&lt; \"Enter password: \";\n    getline(cin, password);\n    for (char c : password) {\n        if (isupper(c)) {\n            has_upper = true;\n        }\n        if (islower(c)) {\n            has_lower = true;\n        }\n        if (isdigit(c)) {\n            has_digit = true;\n        }\n        if (ispunct(c)) {\n            has_punct = true;\n        }\n    }\n    if (has_upper &amp;&amp; has_lower &amp;&amp; has_digit &amp;&amp; has_punct &amp;&amp; password.length() &gt;= 8) {\n        cout &lt;&lt; \"Password is strong.\" &lt;&lt; endl;\n    }\n    else {\n        cout &lt;&lt; \"Password is weak.\" &lt;&lt; endl;\n    }\n    return 0;\n}\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre><code class=\"language-cpp line-numbers\">Enter password: gdhd\nPassword is weak.\n<\/code><\/pre>\n<p><strong>\u89e3\u91ca:<\/strong><\/p>\n<p>\u5728\u4e0a\u9762\u7684C++\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5404\u79cd\u53c2\u6570\u9a8c\u8bc1\u5bc6\u7801\u3002\u5982\u679c\u5bc6\u7801\u5305\u542b\u5927\u5199\u5b57\u6bcd\u3001\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u6807\u70b9\u7b26\u53f7\u7b49\uff09\uff0c\u5219\u88ab\u8ba4\u4e3a\u662f\u5f3a\u5bc6\u7801\u3002\u6211\u4eec\u8f93\u5165\u4e86\u7528\u6237\u5bc6\u7801\uff0c\u5e76\u68c0\u67e5\u54ea\u4e9b\u53c2\u6570\u4e3a\u771f\uff0c\u54ea\u4e9b\u4e3a\u5047\u3002\u5982\u679c\u6709\u4efb\u4f55\u53c2\u6570\u4e3a\u5047\uff0c\u6211\u4eec\u5c06\u6253\u5370\u51fa\u5bc6\u7801\u662f\u5f31\u7684\uff1b\u5426\u5219\uff0c\u5b83\u662f\u5f3a\u7684\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong> \u8fdb\u884c\u5bc6\u7801\u9a8c\u8bc1\u3002<\/p>\n<p><strong><a href=\"https:\/\/deepinout.com\/cpp\/cpp-top-tutorials\/1696211102_j_cpp-tutorial.html\" data-internallinksmanager029f6b8e52c=\"21\" title=\"C++ \u6559\u7a0b\" target=\"_blank\" rel=\"noopener\">C++<\/a> \u4ee3\u7801:<\/strong><\/p>\n<pre><code class=\"language-cpp line-numbers\">#include \n#include \nusing namespace std;\n\nbool validatePassword(string password)\n{\n    \/\/ regex pattern for password validation\n    regex pattern(\"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@#<span class=\"katex math inline\">%^&amp;+=])(?=\\\\S+<\/span>).{8,}\");\n\n    \/\/ check if the password matches the pattern\n    if (regex_match(password, pattern))\n    {\n        return true;\n    }\n    else\n    {\n        return false;\n    }\n}\n\nint main()\n{\n    string password;\n    cout &lt;&lt; \"Enter password: \";\n    getline(cin, password);\n\n    if (validatePassword(password))\n    {\n        cout &lt;&lt; \"Password is valid.\" &lt;&lt; endl;\n    }\n    else\n    {\n        cout &lt;&lt; \"Password is invalid.\" &lt;&lt; endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 <strong>validatePassword<\/strong> \uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u5bc6\u7801\u662f\u5426\u6709\u6548\u3002\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a <strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong> \u6a21\u5f0f\u6765\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u6ee1\u8db3\u4e00\u5b9a\u7684\u8981\u6c42\uff1a<\/p>\n<p><strong>(?=.*[a-z])\uff1a<\/strong> \u5bc6\u7801\u5fc5\u987b\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>(?=.*[A-Z])\uff1a<\/strong> \u5bc6\u7801\u5fc5\u987b\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>(?=.*[0-9])\uff1a<\/strong> \u5bc6\u7801\u5fc5\u987b\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p><strong>(?=.*[@#$%^ &amp;+=])\uff1a<\/strong>\u5bc6\u7801\u5fc5\u987b\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<p><strong>(?=.*&#92;\\S+$)\uff1a<\/strong> \u5bc6\u7801\u4e0d\u80fd\u5305\u542b\u4efb\u4f55\u7a7a\u683c\u3002<\/p>\n<p><strong>.{8,}\uff1a<\/strong> \u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u81f3\u5c11\u4e3a8\u4e2a\u5b57\u7b26\u3002<\/p>\n<p>\u5982\u679c\u5bc6\u7801\u4e0e\u6a21\u5f0f\u5339\u914d\uff0c\u5219\u51fd\u6570\u8fd4\u56detrue\u3002\u5426\u5219\uff0c\u8fd4\u56defalse\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u8fdb\u884c\u5bc6\u7801\u9a8c\u8bc1 \u5bc6\u7801\u9a8c\u8bc1\u662f\u7f51\u7edc\u5b89\u5168\u4e2d\u4e00\u4e2a\u7ecf\u5e38\u88ab\u5ffd\u89c6\u7684\u91cd\u8981\u65b9\u9762\u3002\u5bc6\u7801\u662f\u9632\u6b62\u672a\u77e5\u8bbf\u95ee\u8d26\u6237\u6216\u7cfb\u7edf\u7684\u7b2c\u4e00\u9053\u9632\u7ebf\uff0c\u786e\u4fdd\u5176\u5f3a\u5ea6\u53ef\u4ee5\u9632\u6b62\u8bb8\u591a\u7f51\u7edc\u653b\u51fb\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8C++\u4e2d\u7684\u5bc6\u7801\u9a8c\u8bc1\uff0c\u5e76\u4ecb\u7ecd\u5b9e\u73b0\u5bc6\u7801\u9a8c\u8bc1\u7684\u5404\u79cd\u6280\u672f\u548c\u65b9\u6cd5\u3002 \u4ec0\u4e48\u662f\u5bc6\u7801\u9a8c\u8bc1 \u5bc6\u7801 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[847],"tags":[],"_links":{"self":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts\/512019"}],"collection":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/comments?post=512019"}],"version-history":[{"count":1,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts\/512019\/revisions"}],"predecessor-version":[{"id":512020,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts\/512019\/revisions\/512020"}],"wp:attachment":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/media?parent=512019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/categories?post=512019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/tags?post=512019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}