Read number as string from user and compare them as number or string by passing strcmp function as parameter - C String

C examples for String:Compare

Description

Read number as string from user and compare them as number or string by passing strcmp function as parameter

Demo Code

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

void check(char *a, char *b, int(*cmp)(const char *, const char *));
int compvalues(const char *a, const char *b);

int main(void)
{
   char s1[80], s2[80];

   printf("Enter two values or two strings.\n");
   gets_s(s1);/*from  www .j  a  v a2  s .co  m*/
   gets_s(s2);

   if (isdigit(*s1)) {
      printf("Testing values for equality.\n");
      check(s1, s2, compvalues);
   }
   else {
      printf("Testing strings for equality.\n");
      check(s1, s2, strcmp);
   }

   return 0;
}

void check(char *a, char *b,
   int(*cmp)(const char *, const char *))
{
   if (!(*cmp)(a, b)) printf("Equal");
   else printf("Not Equal");
}

int compvalues(const char *a, const char *b)
{
   if (atoi(a) == atoi(b)) return 0;
   else return 1;
}

Result


Related Tutorials