编程比较用户键盘输入的口令与内设的口令secret是否相同。若相同,则输出"Correct password! Welcome to the system…\n",若userInput>password则输出"Invalid password!user input>password\n",否则输出"Invalid password!user input<password\n"。
输入提示信息:“Input Password:”
输入格式:"%s"
输出提示信息:
“Correct password! Welcome to the system…\n”
“Invalid password!user input<password\n”
“Invalid password!user input>password\n”
#include <stdio.h>
#include <string.h>
int main(void)
{
char pwd[50];
printf("Input Password:");
gets(pwd);
int swi=strcmp(pwd,"secret");
/*虽然在函数说明中推荐使用字符数组名作为参数,
但是凭直觉就知道能够用双引号直接引用一个字符串进行比较。
当前>后输出正数,前=后输出0,前<后输出负数*/
if(swi==0)
{
printf("Correct password! Welcome to the system...\n");
}
else if(swi>0)
{
printf("Invalid password!user input>password\n");
}
else{
printf("Invalid password!user input<password\n");
}
}