[C语言]口令比较填空:阅读程序,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。 下面程序比较用户键盘输入的口令userInput与内设的口令password是否相同。若相同,

阅读程序,按要求在空白处填写适当的表达式或语句,使程序完整并符合题目要求。
   下面程序比较用户键盘输入的口令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;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值