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
What are qualifiers?
Can we use visual studio for c?
What is ctrl c called?
if the area was hit by a virus and so the decrease in the population because of death was x/3 and the migration from other places increased a population by 2x then annually it had so many ppl. find our the population in the starting.
What is keyword in c?
What is the g value paradox?
What are the different types of pointers used in c language?
Explain the difference between malloc() and calloc() in c?
a formula,a series of steps,or well defined set of rules for solving a problem a) algorithem b) program c) erdiagram d) compiler
What is the use of #define preprocessor in c?
An instruction which is analysed and acted upon by the processor prior to the compiler going its work a) directive b) constructive c) constant d) absolute mode
Tell me what are bitwise shift operators?
Explain what is the difference between text files and binary files?
What is dynamic dispatch in c++?
Explain what is the benefit of using #define to declare a constant?