阅读程序,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。
下面程序比较用户键盘输入的口令userInput与内设的口令password是否相同。若相同,则输出"Correct password! Welcome to the system...",若password < userInput,则输出"Invalid password! user input<password",否则输出"Invalid password! user input>password"。
#include <stdio.h>
#include <string.h>
int main()
{
char password[7]= "secret"; //内设的口令
char userInput[81];
printf("Input Password:");
scanf("%s", userInput);
if(___①___)
printf("Correct password! Welcome to the system... \n");
else if (___②___)
printf("Invalid password! user input<password \n");
else
printf("Invalid password! user input>password \n");
return 0;
}
此题可用strcmp函数解决:
strcmp(str1,str2):比较str1与str2,若str1<str2,返回负数 ,若str1>str2,返回正数 ,若str1=str2,返回0
所以:
#include <stdio.h>
#include <string.h>
int main()
{
char password[7]= "secret"; //内设的口令
char userInput[81];
printf("Input Password:");
scanf("%s", userInput);
if( strcmp(password,userInput)==0 )
printf("Correct password! Welcome to the system... \n");
else if (strcmp(userInput,password)<0)
printf("Invalid password! user input<password \n");
else
printf("Invalid password! user input>password \n");
return 0;
}