strstr not functioning

前端 未结 2 1354
离开以前
离开以前 2020-11-30 12:25

Why does this particular piece of code return false on the strstr() if I input \"test\"?

char input[100];

int main()
{
    fgets(input, 100, stdin);
    pri         


        
2条回答
  •  感情败类
    2020-11-30 12:46

    Add input[strlen(input) - 1] = '\0'; after the fgets. fgets reads in the newline char ('\n'). There is no '\n' in "test message" so input will never be contained within it.

    You should really check to see if the newline is at the end of the buffer after calling fgets to know if the whole line was able to actually fit into it, and also to obviously remove it.

提交回复
热议问题