我必须将字符串“death”与文本文件中的任意五个字符串进行比较。
我似乎无法让我的功能正常工作,但我看不出我做错了什么。有人有什么建议吗?
*注意:我的 strcmp 只返回 -1 或 1 但从不返回 0
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//Function to check if strings are a match regardless of case
bool doesMatch (char testText[], char testDeath[]) {
if (strcasecmp(testDeath, testText) == 0) {
return true;
}
else
return false;
}
int main (int argc, char *argv[]) {
char test1[5] = {getchar(), getchar(), getchar(), getchar(), getchar()};
bool testMatch;
char test2[5] = {'d','e','a','t','h'};
//Test arrays until End of FIle
while (test1[4] != EOF) {
testMatch = doesMatch(test1, test2);
if (testMatch == true) {
printf ("Match!\n");
}
//"slide" array down one character
test1[0] = test1[1];
test1[1] = test1[2];
test1[2] = test1[3];
test1[3] = test1[4];
test1[4] = getchar();
}
return 0;
}