在 C 中比较密码的两个字符字符串
Comparing two char strings for a password in C
我一直在尝试比较C中的两个char变量,一个是关键字,另一个是用户输入的密码,但尽管两者相同,但它不让我进入。这是代码:
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include <string.h>
int main(){
char pass[4], key[4]="tres";
start:
clrscr();
printf("Write your password: ");
scanf("%s", &pass);
if (strcmp(pass,key)!= 0){
printf("\nWrong password, try again.");
getch();
goto start;
}else if(strcmp(pass,key) == 0){
printf("Welcome!");
getch();
clrscr();
//here goes the rest of the program
}
return 0;
}
strcmp
适用于空终止字符串。每个字符串的末尾需要一个 [=11=]
字符,在这种情况下每个字符串的长度应为 5 个字符。
key[5]="tres";
您必须将数组的大小增加 1 才能存储字符串字符的结尾 [=11=]
我一直在尝试比较C中的两个char变量,一个是关键字,另一个是用户输入的密码,但尽管两者相同,但它不让我进入。这是代码:
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include <string.h>
int main(){
char pass[4], key[4]="tres";
start:
clrscr();
printf("Write your password: ");
scanf("%s", &pass);
if (strcmp(pass,key)!= 0){
printf("\nWrong password, try again.");
getch();
goto start;
}else if(strcmp(pass,key) == 0){
printf("Welcome!");
getch();
clrscr();
//here goes the rest of the program
}
return 0;
}
strcmp
适用于空终止字符串。每个字符串的末尾需要一个 [=11=]
字符,在这种情况下每个字符串的长度应为 5 个字符。
key[5]="tres";
您必须将数组的大小增加 1 才能存储字符串字符的结尾 [=11=]