Skip to content
Related Articles

Related Articles

Print * in place of characters for reading passwords in C
  • Difficulty Level : Easy
  • Last Updated : 20 Sep, 2018
GeeksforGeeks - Summer Carnival Banner

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 program to print * 
// in place of characters
#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();
}

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.

This article is contributed by Pavan Gopal Rayapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :