C Program to Compare two Strings

C Program to Compare two Strings

C program to count a number of words in a string; Through this tutorial, we will learn how to count number of words in a given string using standard method, function, and standard library in c.

Programs to Compare two Strings in C

Use the following programs to count number of words in a given string using standard method, function, and standard library in c:

  • C Program to Compare two Strings using Standard Method
  • C Program to Compare two Strings using Function
  • C Program to Compare two Strings using Standard Libarry

C Program to Compare two Strings using Standard Method

#include <string.h>
 
int main()
{
    char s1[1000],s2[1000];  
    int i,c=0;
 
    printf("Enter  string1: ");
    gets(s1);
    printf("Enter  string2: ");
    gets(s2);
    if(strlen(s1)==strlen(s2))
    {
    	for(i=0;s2[i]!='
#include <string.h>
int main()
{
char s1[1000],s2[1000];  
int i,c=0;
printf("Enter  string1: ");
gets(s1);
printf("Enter  string2: ");
gets(s2);
if(strlen(s1)==strlen(s2))
{
for(i=0;s2[i]!='\0';i++)  
{
if(s1[i]==s2[i])
c++;
}
if(c==i)
printf("strings are equal");
else
printf("strings are not equal");
}
else
printf("strings are not equal");
return 0;
}
';i++) { if(s1[i]==s2[i]) c++; } if(c==i) printf("strings are equal"); else printf("strings are not equal"); } else printf("strings are not equal"); return 0; }

The output of the above c program; as follows:

Enter  string1: hello
Enter  string2: hello
strings are equal

C Program to Compare two Strings using Function

#include <stdio.h>
#include <string.h>
 
int stringcompare(char *s1,char *s2)
{
	int i,c=0;
	if(strlen(s1)==strlen(s2))
    {
    	for(i=0;s2[i];i++)  
        {
        	if(s1[i]==s2[i])
        	 c++;
 	    }
 	    if(c==i)
          return 1;
          
    }
    return 0;
     
 	
}
int main()
{
 
    char s1[1000],s2[1000],c;  
 
    printf("Enter  string1: ");
    gets(s1);
    printf("Enter  string2: ");
    gets(s2);
    c=stringcompare(s1,s2);
    if(c)
        printf("strings are equal");
    else
        printf("strings are not equal");
    
	return 0;
    
}

The output of the above c program; as follows:

Enter  string1: test
Enter  string2: test2
strings are not equal

C Program to Compare two Strings using Standard Library

#include <stdio.h>
#include <string.h>
 
 
int main()
{
   char s1[1000],s2[1000];  
 
    printf("Enter  string1: ");
    gets(s1);
    printf("Enter  string2: ");
    gets(s2);
    if(!strcmp(s1,s2))
        printf("strings are equal");
    else
        printf("strings are not equal");    
 
    return 0;
    
}

The output of the above c program; as follows:

Enter  string1: class
Enter  string2: class
strings are equal

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *