C Program to Compare Two Strings

Write C Program to Compare Two Strings

// C Program to Compare Two Strings

#include <stdio.h>
#include <string.h>

int main()
{
	char S1[] = "CodeCrucks",S2[] = "CodeCrucks";
	int i = 0,j = 0,flag = 0;

	// Using inbuilt function
//	if(strcmp(S1,S2)==0)
//	 	printf("Strings are equal");
//	else
//	 	printf("Strings are not equal");


	while(S1[i] != '\0' && S2[i] != '\0')
	{
	    if(S1[i] != S2[i])
	    {
                flag = 1;
		break;
            }
	    i++;
	}

	printf("String 1 :--> %s", S1);
	printf("\nString 2 :--> %s", S2);

	if(flag == 0)
		printf("\n\nStrings are equal ");
	else
		printf("\n\nStrings are not equal ");

	return 0;
}

Output:

Run 1:
String 1 :--> Code
String 2 :--> Crucks

Strings are not equal

Run 2:
String 1 :--> CodeCrucks
String 2 :--> CodeCrucks

Strings are equal

≪ Previous | Next ≫