Write a program to read two strings and compare them using the function strcmp( ) and print a message that the first string is equal, less, or greater than the second one.
#include <stdio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("Enter two strings to compare them\n");
scanf("%s %s",str1,str2);
if(strcmp(str1,str2)>0)
printf("First string is Greater than the Second string!");
if(strcmp(str1,str2)<0)
printf("First string is Less than the Second string!");
if(strcmp(str1,str2)==0)
printf("First and Second strings are Equal!");
getch();
}
OUTPUT
Enter two strings to compare them
Ramesh Rakesh
First string is Greater than the Second string!
Enter two strings to compare them
Rakesh Ramesh
First string is Less than the Second string!
Enter two strings to compare them
Rakesh Rakesh
First and Second strings are Equal!
#include <stdio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("Enter two strings to compare them\n");
scanf("%s %s",str1,str2);
if(strcmp(str1,str2)>0)
printf("First string is Greater than the Second string!");
if(strcmp(str1,str2)<0)
printf("First string is Less than the Second string!");
if(strcmp(str1,str2)==0)
printf("First and Second strings are Equal!");
getch();
}
OUTPUT
Enter two strings to compare them
Ramesh Rakesh
First string is Greater than the Second string!
Enter two strings to compare them
Rakesh Ramesh
First string is Less than the Second string!
Enter two strings to compare them
Rakesh Rakesh
First and Second strings are Equal!
No comments:
Post a Comment