c - How to do strcmp correctly? -


i have compare string "death" to 5 character string in text file.

i can't seem function work can't see doing wrong. have suggestion?

*notes: strcmp returns -1 or 1 never 0

#include <stdio.h> #include <stdbool.h> #include <string.h>  //function check if strings 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 1 character       test1[0] = test1[1];       test1[1] = test1[2];       test1[2] = test1[3];       test1[3] = test1[4];       test1[4] = getchar();   }   return 0; } 

as havenard said, strcmp() requires null-terminated strings, means each string needs end character '\0'. if insist on piecing strings yourself, have remember append null character @ end of each 1 in order perform string functions on them.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -