Question

I'm making a program that lets user enter a string then program searches various words in the string using strstr function, then calls different functions according to which word is found.I decided to use switch statements to check which words are present.I made a prototype program as:

int main() {
    char str[] = "This is a string.";
    char str1[] = "is";
    int num = strstr(str, str1);

    switch(num) {
        case 0:
            cout<<"Str1 is present";
            break;
        case -1:
            cout<<"str1 is absent";
            break;
    }
}

It's gimme the error:

invalid conversion from 'char*' to 'int' [-fpermissive]

What am I doing wrong?

Was it helpful?

Solution

strstr returns a char* or const char*

Have a read here.

char str1 = "This is a string.";
char str2 = "is";
char* result = strstr(str1, str2);
if (result == NULL)
{
    cout<<"str1 is absent";
}
// etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top