我必须将字符串“death”与文本文件中的任意五个字符串进行比较。如何正确执行strcmp?
我似乎无法让我的函数来工作,但我看不到我在做什么错。任何人有任何建议?
*注:我只的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;
}
'的strcmp()'只能比较空终止字符串。 – Havenard 2013-04-05 01:34:37
如果你正在使用'testMatch ==真',我认为这应该被标记为C++,不C. – 2013-04-05 01:38:11
究竟你“我只的strcmp -1或1,但从来没有返回0”是什么意思?你实现了你自己的strcmp,它应该返回0但不是? – Kevin 2013-04-05 01:42:01