简体   繁体   中英

strstr in switch statements

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM