C Program to Compare two Strings

This C program compares two string and tell if they are equal or not. We are taking two strings here str and str1. It is an array with 30 indexes. There are two int variables i and j which will be used to evaluate the strings. The while loops runs upto end of str[] string, ‘\0’ defines the null character, which is added at the end of the string in c. If the strings are equal, the value for j variable will be changed to 0, which specifies that the strings are equal. If the loop encounters a letter which does not match which the letter of another string, the program breaks out of the loop, printing “Not Equal”.

Code:
#include
void main(){
int i,j=1;
char str[30],str1[30];
printf("Enter two strings:\n");
scanf("%s",str);
scanf("%s",str1);
i=0;
while(str[i]!='\0'){
if(str[i]!=str1[i]){
	printf("Not Equal\n");
	break;
	
	}
	else{
	j=0;
	}
	i++;
}
if(j==0)
printf("Strings are Equal");

}

OUTPUT:

Leave a Reply

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

%d bloggers like this: