Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

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 155 views
2 answers 782 views
1 answer 112 views
112 views asked Dec 16, 2022 by avibootz
1 answer 236 views
1 answer 90 views
1 answer 109 views
109 views asked Aug 23, 2024 by avibootz
1 answer 113 views
...