#strcmp #strcmpfunctioninc #strcmp()inc
C program to Compare Two Strings without Using strcmp() function
C program to Compare Two Strings Using strcmp() function
C program to Compare 2 Strings without Using strcmp() function
C program to Compare 2 Strings Using strcmp() function
• It is used to compare 2 strings character by character.
• strcmp( ) returns an integer value.
• strcmp( ) returns 0(zero) when both strings are equal.
• strcmp( ) returns positive value if string1string2.
• strcmp( ) returns negative value if string1string2.
Ex1:- C program to compare 2 strings using library function
#includestdio.h
#includeconio.h
#includestring.h
int main()
{
char a[10],b[10];
int i;
clrscr();
printf("\nEnter first string\n");
gets(a);
printf("\nEnter second string\n");
gets(b);
i=strcmp(a,b);
if(i==0)
printf("\nBoth strings are equal");
else
printf("\nBoth strings are not equal");
return 0;
}
Result:-
Input:-
Enter First string
Ram
Enter second string
Ram
Output:-
Both strings are equal
Ex2:- C program to compare 2 strings with out using library function
#includestdio.h
#includeconio.h
int main()
{
char a[10],b[10];
int i;
clrscr();
printf("\nEnter first string\n");
gets(a);
printf("\nEnter second string\n");
gets(b);
i=0;
while(a[i]= =b[i] && a[i]!='\0' && b[i]!='\0')
i++;
if(a[i]=='\0' && b[i]=='\0')
printf("\nBoth strings are equal");
else
printf("\nBoth strings are not equal");
return 0;
}
Result:-
Input:-
Enter First string
Ram
Enter second string
Babu
Output:-