Graphics in C - Program to Generate Captcha Code
Here is an interesting
graphics program in C computer language for generating ‘captcha’ code. Please type this program in your editor.
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int gd = DETECT, gm, i, n;
char str[10], str1[20], ch;
initgraph(&gd, &gm,
"c:\\tc\\bgi");
randomize();
while(1)
{
cleardevice();
clrscr();
settextstyle(BOLD_FONT, HORIZ_DIR, 2);
setcolor(RED);
rectangle(10, 40, 250, 130);
for(i = 0; i < 8; i++)
{
n = random(3);
if(n == 0)
str[i] = 65 + random(26);
else if(n == 1)
str[i] = 97 + random(26);
else if(n == 2)
str[i] = 48 + random(10);
}
str[i] = '\0';
outtextxy(20, 60, str);
puts("Enter the captcha
code:");
gets(str1);
settextstyle(SANS_SERIF_FONT,
HORIZ_DIR, 2);
setcolor(BLUE);
if(strcmp(str, str1) == 0)
outtextxy(20, 150,
"Right code!");
else
outtextxy(20, 150,
"Wrong code!");
settextstyle(DEFAULT_FONT,
HORIZ_DIR, 1);
setcolor(BLUE);
outtextxy(20, 230, "Press any
key to change the code…");
outtextxy(20, 260, "Press
<esc> to exit…");
ch = getch();
if(ch == 27)
break;
}
closegraph();
restorecrtmode();
}
Compile and execute the
program. Output would be a screen displaying a ‘captcha’ code, and you would be prompted to enter this code. If you
enter the right code, a message ‘Right
Code!’ will appear on the screen. If you enter the wrong code, message ‘Wrong Code!’ will be displayed. You can
change the ‘captcha’ code by
pressing any key. You can terminate the program by pressing the <esc> key. For this, appropriate
messages have been displayed on the screen.
In the program, firstly, I
have used a macro randomize(). This will initialise a
random number generator with a random value. For this macro, I have included
the header file “time.h”.
randomize();
After this, I have taken a ‘while
true’ loop. This loop would be executed endlessly, until it is
terminated by a ‘break’ statement. User is prompted to enter the <esc> key to terminate the
program. The ASCII value for <esc> key is 27. An ‘if’ construct checks whether the ASCII value of the key pressed is 27. If true, program comes out of the ‘while true’ loop,
executes the remaining instructions, and then terminates.
Inside the ‘while
true’ loop, function cleardevice() clears the screen
in graphics mode, function clrscr() clears the screen in
text mode. I have used this function, because there are some input/output
functions of text mode in the program. Then, set the text style, colour, etc.
and draw a rectangle to enclose the ‘captcha’
code.
settextstyle(BOLD_FONT, HORIZ_DIR, 2);
setcolor(RED);
rectangle(10, 40, 250, 130);
A ‘for’ loop has been taken
to generate the ‘captcha’ code. This
loop executes eight times. In every iteration, a random number among 0, 1, and
2 is generated and stored in the variable ‘n’.
Depending on the value of ‘n’, a
string is populated with alphabets and digits. In the first iteration of the ‘for’
loop, ‘n’ is given a random value
between 0 and 2. If value is picked 0, then, first element of the string would
be a capital letter from A to Z. ASCII value of A
is 65, and number of alphabets is 26. Thus, (65 + a random number between 0
and 25) would give the ASCII value of a capital letter. The
same way, if the value of ‘n’ is
picked 1, then, the first element of
the string would be an alphabet in lower case, because ASCII value of ‘a’ is 97. If the value of ‘n’ is picked 2, then, the first element of the string would be a digit between 0 and 9 (ASCII value of ‘0’ is 48). I have taken 3 random values for this purpose, because there
are 3 types of characters are to be filled in a string of ‘captcha’ code, namely upper-case alphabets, lower-case alphabets,
and numeric digits. You can give different order for these characters in the ‘if’
construct. After giving 8 characters by ‘for’ loop, a null character(‘\0’) has been inserted into the string.
It is necessary to place a null character(‘\0’)
into the string in the last to mark its end. Then the string is being displayed
using function outtextxy().
for(i = 0; i < 8; i++)
{
n = random(3);
if(n == 0)
str[i] = 65 + random(26);
else if(n == 1)
str[i] = 97 + random(26);
else if(n == 2)
str[i] = 48 + random(10);
}
str[i] = '\0';
outtextxy(20, 60, str);
Then, user is being prompted
to enter the ‘captcha’ code by using
two string functions puts() and gets().
Function puts() displays the specified string,
and function gets() takes the input from the
keyboard into the specified string variable.
puts("Enter the captcha
code:");
gets(str1);
After this, the two strings,
‘str’ and ‘str1’, having the program generated ‘captcha’ code and the user entered ‘captcha’ code, are compared. If both strings are same, message ‘Right Code!’ is displayed, otherwise message
‘Wrong Code!’ will appear on the
screen.
if(strcmp(str, str1) == 0)
outtextxy(20,
150, "Right code!");
else
outtextxy(20, 150, "Wrong
code!");
Therefore, this way, you can
implement ‘captcha’ code in your
program to check whether user of your application is a human or not.
So, this simple C language program
is now well explained. For the functions and terms not explained in this post,
please refer my previous posts.
OUTPUT:
OUTPUT:
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment