Program to Compare Two Strings Without using strcmp ()

Whenever we are asked to write a program to compare two strings, we make use of inbuilt function- strcmp (). Obviously, it is the easy method and we can use that. But sometimes, we are asked not to use the strcmp (). This program helps to compare two strings without using strcmp ()

#include<stdio.h>

#include<conio.h>

Void main()

{

char string1[5],string2[5];

int i,temp = 0;

printf(“Enter the  1st string :\n”);

gets(string1);

printf(“\n Enter the 2nd String :\n”);

gets(string2);

for(i=0; string1[i]!=’\0′; i++)

{

if(string1[i] == string2[i])

temp = 1;

else

temp = 0;

}

if(temp == 1)

printf(“Both the strings entered  are same.”);

else

printf(“Both the strings entered are not same.”);

getch();

}

Mohit Arora
Follow me