Write a program to compare two strings without using the
strcmp() function
Answer Posted / kms
The following code compares the strings alphabetically:
For example:
1. "bite" is greater than "apple" even though length of bite
is less than apple. ( it is greater lexicographically)
2. "It is good" is lower than "It is great".
CODE -->
#include<stdio.h>
#include<conio.h>
void strings_compare(char [], char []);
void main()
{
char str1[50], str2[50];
clrscr();
flushall();
printf("String 1 : ");
gets(str1);
flushall();
printf("String 2 : ");
gets(str2);
strings_compare(str1,str2);
getch();
}
void strings_compare(char str1[], char str2[])
{
int i=0,j=0,flag1=0,flag2=0;
printf("\nString 1 : ");
puts(str1);
printf("\nString 2 : ");
puts(str2);
printf("\n");
while(str1[i] != '\0' || str2[j] != '\0')
{
if(str1[i] < str2[j])
{
flag1 = 1;
break;
}
if(str1[i] > str2[j])
{
flag2 = 1;
break;
}
else
{
i++;
j++;
}
}
if(flag1==1)
{
printf("\n\ns1 : %s is lower than s2 : %s",str1,str2);
}
else if(flag2 == 1)
{
printf("\n\ns1 : %s is greater than s2 : %s",str1,str2);
}
else
{
printf("\n\nBoth strings are equal...");
}
}
Is This Answer Correct ? | 6 Yes | 3 No |
Post New Answer View All Answers
Which type of language is c?
program to find out date after adding 31 days to a date in the month of febraury also consider the leap year
Explain how are 16- and 32-bit numbers stored?
Is sizeof a keyword in c?
Can stdout be forced to print somewhere other than the screen?
Is exit(status) truly equivalent to returning the same status from main?
What is calloc in c?
How can you access memory located at a certain address?
what are # pragma staments?
Explain is it better to bitshift a value than to multiply by 2?
What is null in c?
provide an example of the Group by clause, when would you use this clause
By using C language input a date into it and if it is right?
Explain how can you tell whether a program was compiled using c versus c++?
What is the difference between scanf and fscanf?