[Knife Test 2] password protection, knife Test password protection
[Knife Test 2] password protection
Knowledge:
1. while Loop
2. do-while loop
3. if-else
4. strcmp () function
[Charge]
Knowledge about strcmp () Functions
Prototype: int strcmp (const char * str1, const char * str2 );
Return Value:
<0 |
It indicates that str1 is not equal to str2, and its value is smaller than str2. |
= 0 |
Indicates that str1 is equal to str2. |
> 0 |
Str and str2, and its value is greater than str2 |
Today, we have entered the era of "code slaves" (password slaves). Everyone has a lot of passwords.
For example, you also need a password for logging on to CSND, or for a bank card. Therefore, save the password
Code is very important, otherwise be careful C coins and money are missing.
Now let's compile a program to protect our precious passwords ^-^
Requirements:
(1) The password is not displayed on the interface and replaced by an asterisk.
(2) You can delete incorrect input, that is, change the incorrect input.
(3) Password error prompt. Only three incorrect passwords can be entered. Otherwise, exit forcibly.
// Password protection # include <stdio. h> # include <stdlib. h> // for exit # include <string. h> // for strcmp int main (void) {int count = 0; // calculate the number of inputs charmima [7]; // store the entered password intlength = 0; charletter; while (count <= 3) {printf ("\ n enter your 6-digit password:"); while (length <7) {mima [length ++] = '\ 0'; // assign a value to the mima array' \ 0'} length = 0; // set the length to 0 letter = '\ 0'; while (letter! = '\ R') // carriage return {letter = getch (); if (letter =' \ B ') & (mima [0] = '\ 0') {length = 0;} else {if (letter =' \ B ') & (mima [0]! = '\ 0') {printf ("\ B"); // return a printf (""); // clear the incorrect letter printf ("\ B "); // return length --; mima [length] = '\ 0';} else {if (length <6) {printf ("*"); mima [length ++] = letter ;}}} mima [6] = '\ 0'; count ++; if (strcmp ("111111", mima) = 0) {printf ("\ n login successful, welcome to use ^-^ \ n"); break;} else {printf ("\ n \ nSorry, incorrect password entered ~ _~ \ N "); if (count> = 3) {printf (" \ n to protect your account, please contact our Customer Service as soon as possible, thank you for your cooperation \ n "); printf (" \ n Customer Service Hotline: 111-11111111 \ n "); exit (1) ;}printf (" However, you still have % d chances, try again \ n ", 3-count) ;}} return0 ;}
Running result:
[Smile at your fingertips] errors are inevitable. I hope you can get your corrections ^-^
Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself