Hit9 Blog Wiki Project Links Archives Resumé
Page: First UP Pre Next Back

C语言密码输入回显星号

Fork me on GitHub

允许转载, 但转载请注明出处

Date:2012-10-18

来自careercup

原问题:

Write a C program to display * for each letter of password you type in command line.

Like:Password
********

我给出的解决方案:

#include <curses.h>
int main()
{
    initscr(); 
    char c;
    noecho(); 
    while((c = getch()) != '\n') {
        printw("*");
    }
    endwin(); 
    return 0;
}

另外linux 下有专门的函数, 屏蔽回显, 但是不会显示星号

#include <stdio.h>
#include <unistd.h>
int main()
{
    char *passwd = getpass("password:"); 
    printf("%s\n", passwd);
    return 0;
}

Support:mkdwiki