To compare the two strings using C


Levels of difficulty: / perform operation:

C Program to compare the two strings

C Program

#include<stdio.h>
#include<conio.h>
 
void main()
{
	int count1=0,count2=0,flag=0,i;
	char str1[10],str2[10];
 
	clrscr();
	puts("Enter a string:");
	gets(str1);
 
	puts("Enter another string:");
	gets(str2);
 
	while (str1[count1]!='\0')
	count1++;
 
	while (str2[count2]!='\0')
	count2++;
	i=0;
 
	while ( (i < count1) && (i < count2))
	{
		if (str1[i] == str2[i])
		{
			i++;
			continue;
		}
		if (str1[i]<str2[i])
		{
			flag = -1;
			break;
		}
		if (str1[i] > str2[i])
		{
			flag = 1;
			break;
		}
	}
	if (flag==0)
		printf("Both strings are equal\n");
	if (flag==1)
		printf("String1 is greater than string2\n", str1, str2);
	if (flag == -1)
		printf("String1 is less than string2\n", str1, str2);
	getch();
}


Output