Print * in place of characters for reading passwords in C
Last Updated :
16 Feb, 2023
While writing a C program, if you want to type password and it should not be visible on screen or an * symbol is to be printed. Examples:
Input : abcdefg
Output : *******
Note : Below solution uses getch() which may not work on all compilers as this is a non-standard function.
c
#include<stdio.h>
#include<conio.h>
int main( void ){
char password[55];
printf ("password:\n");
int p=0;
do {
password[p]=getch();
if (password[p]!= '\r' ){
printf ("*");
}
p++;
} while (password[p-1]!= '\r' );
password[p-1]= '\0' ;
printf ("\nYou have entered %s as password.",password);
getch();
}
|
Time Complexity: O(n)
Here n is the length of the password
Auxiliary Space: O(n)
The extra space is used to store the password.
Explanation: Basically it is taking the characters we enter through getch() function and print * instead of it for every letter we type. Remark: It doesn’t run in this IDE, download this file and run in your terminal.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...