C Code for String matching[Head First C] doesn't seem to work

后端 未结 2 915
礼貌的吻别
礼貌的吻别 2020-12-21 05:39
#include 
#include 

char tracks[][80] = {
\"I left my heart in Harvard Med School\",
\"Newark, Newark - a wonderful town\",
\"Dancing         


        
2条回答
  •  遥遥无期
    2020-12-21 06:37

    The problem with the code above is that it doesn't account for the fact that fgets leaves the newline in the string. So when you type town and hit enter, you'll end up searching for "town\n".

    A cheap way to solve this would be to fix the string after calling fgets

    search_for[strlen(search_for) - 1] = 0;
    

提交回复
热议问题