Program to compare two strings without using library function strcmp
#include<stdio.h>
#include<string.h>
void main()
{
int len1, len2, flag = 0, i;
char s1[20], s2[20];
printf("\nEnter s1: ");
gets(s1);
printf("\nEnter s2: ");
gets(s2);
len1 = strlen(s1);
len2 = strlen(s2);
if(len1 != len2)
{
flag = 1;
}
else
{
for(i=0;i<len1;i++)
{
if(s1[i] != s2[i])
{
flag = 1;
break;
}
}
}
if(flag == 0)
printf("\nStrings are same.");
else
printf("\nStrings are different.");
}
Output:
Case 1:
Enter s1: abcde
Enter s2: abcdef
Strings are different.
Case 2:
Enter s1: abcde
Enter s2: abfgh
Strings are different.
Case 3:
Enter s1: abcde
Enter s2: abcde
Strings are same.
No comments:
Post a Comment