我用C语言编一道题,我想让密码用*号代替,能实现吗?
时间:2008-05-10 09:00:01
来源:论坛整理 作者: 编辑:chinaitzhe
网友回复:晕,似乎,之前有人问过耶!
网友回复:#include"stdio.h"
#include"conio.h"
void main()
{
int i=0;
char temp;
do
{
temp=getch();
if(temp=='\r') /*判定用户是否为回车*/ break;
if(temp=='\b') /*判定用户输入是否为退格*/
{
if(i>0)
i--;
else
if(i <=0)
continue;
printf("\b \b");
}
else
{
paskey[i ]=temp;
printf("*");
}
}while(1);
paskey[i]='\0';
}
网友回复:是控制台程序还是窗口程序
网友回复:哦,忘记定义数组了,以上的paskey[],你可以定义足够大的容量!
网友回复:在C语言下编写应该是控制台吧?
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ char psd[6]={0}; int i=0; .... while(i <5) { psd[i]=getch(); //接受输入并不回显 printf( '* '); //回显一个 * 号 } ....
网友回复:可以
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include <conio.h> int main(int argc, char *argv[]) { char pass[100]; int i = 0; char ch; while((ch = getch()) != '\n') { //getch() 读取一个字符(不回显) pass[i ] = ch; putchar('*'); } pass[i] = 0; return 0; }
网友回复:楼上的程序似乎有错,碰到回车时不会停下
应该改成这样
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include <conio.h> int main(int argc, char *argv[]) { char pass[100]; int i = 0; char ch; memset(pass,0,sizeof(pass)); while(1) { ch = getch(); if(ch=='\r') break; pass[i ] = ch; putchar('*'); } printf("%s",pass); pass[i] = 0; return 0; }
网友回复:很easy啊.
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include<stdio.h> #include<conio.h> #define n 15 int main() { char num[n]={"\0"}; int i = 0; char ch; printf("输入q则退出输入\n"); printf("input you num:\n"); ch = getch(); num[i ] = ch; printf("*"); while(ch != EOF && ch != 'q' && i<n) { ch = getch(); if(ch == 'q') break; num[i ] = ch; printf("*"); } printf("\nnum is:"); for(i=0; i<n; i ) { printf("%c",num[i]); } getch(); return 0; }
网友回复:试了一下!三楼的程序!比较好用!
网友回复:谢谢各位高手的帮助!
网友回复:
三楼似乎就说了一句话?
网友回复:倒,看错,三楼的楼上。
网友回复:哦,对了头文件“”与 <>括起来有什么区别?
网友回复:int main()
{
char pass[100]={0},c;
char i=0;
while( (c=getchar()) != '\n')
{
if(c == '\b')
{
(i==0)?i:i--;
putchar('\b');
}
pass[i] = c;
putchar('*');
}
printf("the password is %s\n", pass);
return 0;
}
网友回复:用""的话,编译器首先会在源文件所在目录下寻找头文件,然后再在编译器目录下寻找头文件;用 <>则刚好相反;所以假如是你自定义的头文件,就用"",假如是引用系统的头文件,就用 <>
网友回复:
系统的用 <>
自定义的用""
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <termio.h> #include <fcntl.h> #include <curses.h> int tty_mode(int how) { static struct termios original_mode; if(how==0) tcgetattr(0,&original_mode); else return tcsetattr(0,TCSANOW,&original_mode); } void set_crmode() { struct termios ttystate; tcgetattr(0,&ttystate); ttystate.c_lflag &= ~ICANON; ttystate.c_lflag &=~ECHO; ttystate.c_cc[VMIN]=1; tcsetattr(0,TCSANOW,&ttystate); } int main() { char passwd[256]; char name[32]; int flag1=1,flag2=0,len,i,j; memset(passwd,0,sizeof(passwd)); while(flag1) { memset(name,0,sizeof(name)); printf("input your name:"); fflush(stdout); scanf("2s",name); len=strlen(name); if(len<4||len>8) { system("echo `expr 4 5 `"); printf("用户名字必须在4--8个字符之间\n"); continue; } for(j=0;j<len;j ) for(i=j 1;i<len;i ) { if(name[i]==name[j]) { i=len; j=len; flag2=1; } } if(flag2) { flag2=0; printf("用户名字不能包含同一个字符\n"); } else break; } printf("Input passwd:"); fflush(stdout); getchar(); i=0; char ch; fflush(stdin); tty_mode(0); set_crmode(); while(ch=(char)getchar()) { if(ch=='\n') { if(i<8) { printf("\n密码长度要求8位以上\n"); memset(passwd,0,sizeof(passwd)); i=0; continue; } else break; } passwd[i ]=ch; printf("*"); fflush(stdout); fflush(stdin); if(i>16) { printf("passwd very long\n"); } } tty_mode(1); printf("\n用户注册成功!! 用户名为 : %s \n\n", name); }
网友回复:
发表于:2008-05-06 20:42:312楼 得分:0
#include"stdio.h"
#include"conio.h"
void main()
{
int i=0;
char temp;
do
{
temp=getch();
if(temp=='\r')
if(temp=='\b')
{
if(i>0)
i--;
else
if(i <=0)
continue;
printf("\b \b");
}
else
{
paskey[i ]=temp;
printf("*");
}
}while(1);
paskey[i]='\0';
}
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ char obj; obj=getch(); putch('*');
网友回复:显示的时候用*号替换
网友回复:记住了.以后可以直接用啦.
网友回复:进来学习.
网友回复:mark
网友回复:温习一下
网友回复:学习
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ /* 函数名: getpass 功 能: 读一个口令 用 法: char *getpass(char *prompt); 程序例: */ #include <conio.h> int main(void) { char *password; password = getpass("Input a password:"); cprintf("The password is: %s\r\n", password); return 0; }
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include "string.h" #include <conio.h> int main(int argc, char *argv[]) { char pass[100]; int i = 0; char ch; memset(pass,0x00,sizeof(pass)); while(ch = getch()) { if (ch == 13 || ch == 27) { break; } pass[i ] = ch; putchar('*'); } pass[i] = 0; printf("\n%s\n",pass); return 0; }
网友回复:study~~
网友回复:mark
网友回复:学习了
网友回复:#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
char pass[100];
int i = 0;
char ch;
memset(pass,0,sizeof(pass));
while(1)
{
ch = getch();
if(ch=='\r')
break;
pass[i ] = ch;
putchar('*');
}
printf("%s",pass);
pass[i] = 0;
return 0;
}
网友回复:严格来说
仅仅用标准的c或者c 是不可能实现你的这个功能的
因为
上面的那些程序一般的只是,有输入然后输出×××
但是你的输入也一样被显示,比如:
输入:
aaa
输出
***
而密码框的要求是:
输入就直接成了×××
比如
输入:
***
要用到库函数吧
18楼的没有尝试,就是对终端编程,具体什么功能不清楚
网友回复: 学习```
网友回复:char ch = getch();
putchar('*');
网友回复:这样的话呢
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ char ch = getch(); putchar('*');
网友回复:只是学习!
网友回复:严格来说
仅仅用标准的c或者c 是不可能实现你的这个功能的
因为
上面的那些程序一般的只是,有输入然后输出×××
但是你的输入也一样被显示,比如:
输入:
aaa
输出
***
而密码框的要求是:
输入就直接成了×××
比如
输入:
***
要用到库函数吧
18楼的没有尝试,就是对终端编程,具体什么功能不清楚
网友回复:C语言当然可以编不是控制台的程序,控制台和C可没关系
网友回复:输入就要转换在输入框里看到的就应该显示*
网友回复:估计18楼的应该可以,但是我个人比较建议用getch代替tty方法,因为tty比较依靠操作系统,而getch一般操作系统都实现了
其实原理无非就是不“回显”
网友回复:还可以这样子。。。
呵呵~~学到一招~~
网友回复:学习ing
!!
网友回复:#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 20
int main( void )
{
char ch;
int i = 0;
char password[MAX];
printf("password:");
while(( ch = getch()) != 3 ) {
if( ch == '\b' ){ /* 假如是退格符 */
if( i == 0 ) /* 假如没有输入任何字符,就不相应,避免删除提示字符 */
;
else { /* 否则清空一个字符 */
putchar('\b');
putchar(' ');
putchar('\b');
i -= 1;
password[i] = '\0'; /* 让最后以个字符为结束符 */
}
}
else if( ch == 13 ){ /* 是回车的话,结束 */
password[i 1] = '\0';
break;
}
else { /* 假如是其他字符,不大于19个,保存到数组 */
if( i >= 19 )
;
else {
password[i] = ch;
putchar('*');
i = 1;
}
}
}
puts(password); /* 看看和你输入的是否一直 */
getch();
return EXIT_SUCCESS;
}
关键字:语言,一道,我想,密码,代替,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面