c - 如何检查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);
}
解决方案
在 C 中,单引号用于字符常量。所以'*'
不是包含字符的字符串,*
而是仅包含字符的字符串*
。
要指定字符串,请使用双引号。
}while(strcmp(pass,"*") != 0);
推荐阅读
- spring - Spring Security LDAP 身份验证抛出 NO_ATTRIBUTE_OR_VAL 错误
- javascript - 如何将 C3 Gauge Chart 集成到 Angular 5 中?
- vtk - 在 C++ 中使用 ITK 读取 Nifti 图像
- php - PHP/SQL 查询不会执行
- java - 为 RecyclerView 中的某些行禁用 ItemTouchHelper 滑动
- css - 无限雪佛龙线
- mysql - Docker 构建:错误:ER_NOT_SUPPORTED_AUTH_MODE:MySQL 客户端
- javascript - 动态创建数组
- php - 将图像保存到 PHP 中的目录?
- php - PHP Pdo SELECT VIEW 查询失败