A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a test to determine whether the user is human or not.

So, the task is to generate unique CAPTCHA every time and to tell whether the user is human or not by asking user to enter the same CAPTCHA as generated automatically and checking the user input with the generated CAPTCHA.

Examples:

CAPTCHA: x9Pm72se Input: x9Pm62es Output: CAPTCHA Not Matched CAPTCHA: cF3yl9T4 Input: cF3yl9T4 Output: CAPTCHA Matched

The set of characters to generate CAPTCHA are stored in a character array chrs[] which contains (a-z, A-Z, 0-9), therefore size of chrs[] is 62.

To generate a unique CAPTCHA every time, a random number is generated using rand() function (rand()%62) which generates a random number between 0 to 61 and the generated random number is taken as index to the character array chrs[] thus generates a new character of captcha[] and this loop runs n (length of CAPTCHA) times to generate CAPTCHA of given length.

`// C++ program to automatically generate CAPTCHA and ` `// verify user ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns true if given two strings are same ` `bool` `checkCaptcha(string &captcha, string &user_captcha) ` `{ ` ` ` `return` `captcha.compare(user_captcha) == 0; ` `} ` ` ` `// Generates a CAPTCHA of given length ` `string generateCaptcha(` `int` `n) ` `{ ` ` ` `time_t` `t; ` ` ` `srand` `((unsigned)` `time` `(&t)); ` ` ` ` ` `// Characters to be included ` ` ` `char` `*chrs = ` `"abcdefghijklmnopqrstuvwxyzABCDEFGHI"` ` ` `"JKLMNOPQRSTUVWXYZ0123456789"` `; ` ` ` ` ` `// Generate n characters from above set and ` ` ` `// add these characters to captcha. ` ` ` `string captcha = ` `""` `; ` ` ` `while` `(n--) ` ` ` `captcha.push_back(chrs[` `rand` `()%62]); ` ` ` ` ` `return` `captcha; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Generate a random CAPTCHA ` ` ` `string captcha = generateCaptcha(9); ` ` ` `cout << captcha; ` ` ` ` ` `// Ask user to enter a CAPTCHA ` ` ` `string usr_captcha; ` ` ` `cout << ` `"\nEnter above CAPTCHA: "` `; ` ` ` `cin >> usr_captcha; ` ` ` ` ` `// Notify user about matching status ` ` ` `if` `(checkCaptcha(captcha, usr_captcha)) ` ` ` `printf` `(` `"\nCAPTCHA Matched"` `); ` ` ` `else` ` ` `printf` `(` `"\nCAPTCHA Not Matched"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

CAPTCHA: cF3yl9T4 Enter CAPTCHA: cF3yl9T4 CAPTCHA Matched

This article is contributed by **Himanshu Gupta(Bagri)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate 0 and 1 with 25% and 75% probability
- Generate integer from 1 to 7 with equal probability
- Generate a random permutation of 1 to N
- Strong Password Suggester Program
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Karger’s algorithm for Minimum Cut | Set 2 (Analysis and Applications)
- Randomized Algorithms | Set 1 (Introduction and Analysis)
- Randomized Algorithms | Set 2 (Classification and Applications)
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Find the player who wins the game by removing the last of given N cards
- Find N random points within a Circle
- Random Numbers Ecosystem in Julia - The Natural Side
- Slow Start Backoff Algorithm for Ad-Hoc
- Maximum String Partition