How to compare strings

前端 未结 3 1314
悲哀的现实
悲哀的现实 2020-12-10 00:55

I wanted to compare a string without actually defining one of them as a string, something like this,

if (string == \"add\")

Do I have to de

3条回答
  •  再見小時候
    2020-12-10 01:23

    You could use strcmp():

    /* strcmp example */
    #include 
    #include 
    
    int main ()
    {
      char szKey[] = "apple";
      char szInput[80];
      do {
         printf ("Guess my favourite fruit? ");
         gets (szInput);
      } while (strcmp (szKey,szInput) != 0);
      puts ("Correct answer!");
      return 0;
    }
    

提交回复
热议问题