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