Password Checking in C

how to write a password checking program in C:

int password();
void main()
{
if(password())
printf("U r a valid user");
else
printf("U r not a valid user");
}


int password()
{
   char ch,str[50];
int i=0;
printf("enter ur password : ");

while(1)
{
        ch=getch();
       
        if(ch==13)
        break;
         printf("*");
        str[i++]=ch;
        }
     
        str[i]='\0';
       

if(strcmp(str,"kumar"))
return 0;
else
return 1;

}





Input : Enter ur password : *****

output : U r a valid user




result analysis:

whenever u r pressing a character/key getch() will be invoked. the character will be stored into ch.
then the character will be transferred to the str[] one by one and during this operation * will be printed on the screen after each key press. this is possible because getch() does not echo the character it has received. when the enter key will be pressed , ch variable will store new line character. ascii value of new line character is 13. whenever 13 will be encountered while loop will be stopped. after coming out from the loop the last index of the str[] will store '\0' to terminate the string. then the string is compared with the password u want to check with.

Comments

Popular posts from this blog

JAVA program to add two distance

Print Pattern using C