×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Find the length of a string - String
compare two string - String - String - C Language
705    Arnab De    15/04/2019   

Write a C program to compare two string

First check the length of two string. if length of the two string are equal then Travel the source strings, and compare the character one by one. If all characters are same then print both strings are identical. We can also use strcmp() or strcmpi() or stricmp() method to compare two string. for that, we must include string.h header file.

softetechnologies
#include<string.h> //for the method strlen()

int main()
{
	char str1[30],str2[30],i,fl=1;
	printf("Enter A String: ");
	gets(str1);
	printf("Enter Another String: ");
	gets(str2);
	
	if(strlen(str1)==strlen(str2))
	{
		for(i=0;str1[i]!='\0';i++)
		{
			if(str1[i]!=str2[i])
			{
				printf("Strings are not equal");
				fl=0;
				break;
			}
		}
		if(fl)
		{
			printf("Strings are equal");
		}
	}
	else
	{
		printf("Strings are not equal");
	}
	return 0;
}
softetechnologies

using strcmp() method

#include<string.h> //for the method strlen() and strcmp()

int main()
{
	char str1[30],str2[30],i,fl=1;
	printf("Enter A String: ");
	gets(str1);
	printf("Enter Another String: ");
	gets(str2);
	
	if(strcmp(str1,str2)==0)
	{
		printf("Strings are equal");
	}
	else
	{
		printf("Strings are not equal");
	}
	return 0;
}
softetechnologies
Find the length of a string - String
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
If you want to go east, don't go west.
Sri Sri Ramakrishna Paramahamsa
2081
25.81
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     31584
28/06/2017     26610
02/08/2017     23330
26/05/2018     21526
15/05/2017     20222
06/07/2017     19001
01/08/2017     17464
21/04/2018     17236
01/07/2017     14866
25/04/2018     14109
softetechnologies