How to compare two strings using strcmp function in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h> 

int main(void)
{
   char s1[10], s2[10];
 
   printf("Enter s1:\n");
   gets(s1);
 
   printf("Enter s2:\n");
   gets(s2);
 
   if (strcmp(s1, s2) == 0 )
       printf("\nequal\n");
   else
       printf("\nnot equal\n");
        
   return 0;
}

/*
run:

Enter s1:
software
Enter s2:
software

equal

*/




answered Sep 14, 2014 by avibootz
edited Sep 14, 2014 by avibootz
0 votes
#include <stdio.h>
#include <string.h> 

int main(void)
{
   char s1[13] = "abc", s2[13] = "Abc";   
   int rv;

   rv = strcmp(s1, s2);

   if (rv < 0)
       printf("s1 is less than s2");
   else if (rv > 0) 
            printf("s2 is less than s1"); // s2 is less than s1 'a' = 97 'A' = 65
        else 
            printf("s1 is equal to s2");
        
   return 0;
}

/*
run:

s2 is less than s1

*/




answered Sep 14, 2014 by avibootz

Related questions

1 answer 173 views
2 answers 804 views
1 answer 126 views
126 views asked Dec 16, 2022 by avibootz
1 answer 255 views
1 answer 108 views
1 answer 137 views
137 views asked Aug 23, 2024 by avibootz
1 answer 130 views
...