首页 > 解决方案 > 如何检查C中的两个字符串是否相同?

问题描述

我刚刚开始学习C并且正在尝试习惯语法,因此当我尝试运行程序并输入值时,它不会返回任何内容。我不知道我做错了什么。任何人都可以帮助我吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char pass[10]; //max length of the string;
    do{
        printf("enter your password: \n");
        scanf("%s",pass);
    }while(strcmp(pass,'*') != 0); //checks if strings are equal

    printf("%s",pass);


}

标签: cstringc-strings

解决方案


在 C 中,单引号用于字符常量。所以'*'不是包含字符的字符串,*而是包含字符的字符串*

要指定字符串,请使用双引号。

 }while(strcmp(pass,"*") != 0);

推荐阅读