You are here

Captcha program in c

This program generates captcha, a captcha is a random code generated using some algorithm. We will use random function in our code. These are used in typing tutors and in website to check whether a human is operating on a website.

C programming code

  1. #include<stdlib.h>
  2. #include<dos.h>
  3. #include<graphics.h>
  4.  
  5. main()
  6. {
  7.    int i = 0, key, num, midx, gd = DETECT, gm;
  8.    char a[10];
  9.  
  10.    initgraph(&gd,&gm,"C:\\TC\\BGI");
  11.  
  12.    midx = getmaxx()/2;
  13.  
  14.    settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
  15.    settextjustify(CENTER_TEXT,CENTER_TEXT);
  16.    setcolor(GREEN);
  17.    outtextxy(midx,20,"CAPTCHA");
  18.    settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
  19.    outtextxy(midx,125,"Press any key to change the generated random code \"captcha\"");
  20.    outtextxy(midx,150,"Press escape key to exit...");
  21.  
  22.    setcolor(WHITE);
  23.    setviewport(100,200,600,400,1);
  24.    setcolor(RED);
  25.    randomize();
  26.  
  27.    while(1)
  28.    {
  29.       while(i<6)
  30.       {
  31.          num = random(3);
  32.  
  33.          if ( num == 0 )
  34.             a[i] = 65 + random(26);     /* 65 is the ASCII value of A */
  35.          else if ( num == 1)
  36.             a[i] = 97 + random(26);     /* 97 is the ASCII value of a */
  37.          else
  38.             a[i] = 48 + random(10);     /* 48 is the ASCII value of 0 */
  39.          i++;
  40.       }
  41.       a[i] = '\0';
  42.       outtextxy(210,100,a);
  43.       key = getch();
  44.  
  45.       if( key == 27 )                     /* escape key*/
  46.          exit(0);
  47.       clearviewport();
  48.       i = 0;
  49.    }
  50. }