Friday, 13 October 2017

Displaying star (*) for input password using c

Here we demonstrate how can we show the star (*) for an input password. It may be used when we are developing an application that has a login form in which we use a user name and password as logging information.

For security purpose it is needed to show (*) instead showing the plain text for the password. The source code is given below to show how we can do it using c/c++.

Source Code:
#include <stdio.h>
#include <conio.h>

int main()
{
     char password[20], ch = 0;
     int i = 0, j;

     printf("Enter the password <limit 20 characters>: ");

     while (i < 20)
     {
           ch = _getch();
           if (ch == `\r`)
                break;
           password[i] = ch;
           i++;
           ch = `*`;
           printf("%c", ch);
     }

     password[i] = `\0`;

     printf("\nThe password is :");

     for (j = 0; j < i; j++)
     {
           printf("%c", password[j]);
     }
     _getch();
     return 0;
}

Output: