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

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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