Compare string with your own function - C String

C examples for String:String Function

Description

Compare string with your own function

Demo Code

#include <stdio.h>
#include <string.h>   // declares strcmp()

#define ANSWER "book2s.com"
#define SIZE 40//from   w ww .  ja v  a2s. com
char * s_gets(char * st, int n);

int main(void)
{
   char try1[SIZE];

   puts("Website:");
   s_gets(try1, SIZE);
   while (strcmp(try1, ANSWER) != 0)
   {
      puts("No, that's wrong. Try again.");
      s_gets(try1, SIZE);
   }
   puts("That's right!");

   return 0;
}


char * s_gets(char * st, int n) {
   char * ret_val;
   int i = 0;

   ret_val = fgets(st, n, stdin);

   if (ret_val) {
      while (st[i] != '\n' && st[i] != '\0')
         i++;
      if (st[i] == '\n')
         st[i] = '\0';
      else // must have words[i] == '\0'
         while (getchar() != '\n')
            continue;
   }
   return ret_val;
}

Related Tutorials