strstr 函数误打误打
strstr function misundersandings
strstr 函数有问题。我试图编译密码程序,但它掉了下来。任何帮助将不胜感激
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
char s[5], password[]="kuku";
int i,k=0;
for (i=0; !k && i<3;i++){
printf(" enter password: n");
gets(s);
if (strstr(s.password[])) k=1;
}
if(k) printf("password accepted");
else printf("password not accepted");
return 0;
}
if (strstr(s.password[](( k=1;
strstr(( 需要两个参数(haystack 和 needle,意思是你想在大海捞针中找到针(。你只传递一个参数,这是错误的。
正确的代码将是这样的:
if (strstr(s, password) != NULL) k=1;
在此表达式中
strstr(s.password[])
有两个错误。第一个是使用点时参数的分隔符是逗号。
第二个是,您必须使用表达式password
而不是构造password[]
考虑到函数strstr
不适合检查两个字符串是否相等。最好使用函数strcmp
。
并且功能gets
不受C标准支持。请改用函数fgets
。
当您尝试将程序编译为C++程序时,请使用标准类std::string
而不是原始字符数组。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- strstr 函数误打误打
- 如何修复对strstr没有匹配函数调用的问题
- strstr函数与2015年相比不起作用
- C++中strstr()函数的时间复杂度、空间复杂度和算法是什么
- 编码是否会影响 strstr() 的结果(和相关函数)
- 使用strstr()函数是错误的
- Strstr()函数重叠字符串搜索
- 使用c++的strstr函数来删除你正在搜索的子字符串的一部分
- strstr() 函数和"rn"