-2

There is a problem with strstr function. I have tried to compile password program, but it falls. Any help would be highly appreciated

#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;
}

2 Answers2

0

if (strstr(s.password[])) k=1;

What did you want to accomplish with this? strstr() needs two arguments (haystack and needle, meaning that you want to find the needle in the haystack). You only pass one argument, which is wrong.

The correct code will be something like:

if (strstr(s, password) != NULL) k=1;
Butuze
  • 36
  • 2
0

In this expression

strstr(s.password[])

there are two errors. The first one is that the separator of arguments is comma while you are using a dot.

The second one is that instead of the construction password[] you have to use the expression password

Take into account that the function strstr is not suitable to check whether two strings are equal each other. It is better to use function strcmp.

And the function gets is not supported by the C standard. Use instead function fgets.

And as you are trying to compile the program as a C++ program then use the standard class std::string instead of raw character arrays.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335