C Program to Compare The Two Strings in Two Methods

Below is a simple C program that compares two strings entered by the user.

Method 1 :

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char input[20];
int i;
printf(“Enter password : “);
gets(input);
i=strcmp(input,”wisdomatrix”);
if(i==0)
{
printf(“\nCorrect password.”);
}
else
{
printf(“\nIncorrect password”);
}
}

Output :

Method 2 :

#include<stdio.h>
int input(char[],char[]);
int main()
{
char str1[100],str2[100];
int compare;
printf(“Enter First string : “);
gets(str1);
printf(“Enter Second string : “);
gets(str2);
compare =stringCompare(str1,str2);
if(compare == 1)
{
printf(“Both strings are equal.”);
}
else
{
printf(“Both strings are not equal”);
}
return 0;
}
int stringCompare(char str1[],char str2[])
{
int i=0,flag=0;
while(str1[i]!=’\0’&&str2[i]!=’\0′)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
i++;
}
if(flag==0&&str1[i]==’\0’&&str2[i]==’\0′)
return 1;
else
return 0;
}

Output :

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights