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
When c language was developed?
HOW TO SOLVE A NUMERICAL OF LRU IN OS ??????
what type of questions arrive in interview over c programming?
Give differences between - new and malloc() , delete and free() ?
What are the properties of union in c?
Can we declare variable anywhere in c?
Write an algorithm for implementing insertion and deletion operations in a singly linked list using arrays ?
How can you convert integers to binary or hexadecimal?
is it possible to create your own header files?
A float occupies 4 bytes in memory. How many bits are used to store exponent part? since we can have up to 38 number for exponent so 2 ki power 6 6, 6 bits will be used. If 6 bits are used why do not we have up to 64 numbers in exponent?
What are structure types in C?
What are the 4 data types?
Do you know what are the properties of union in c?
How are strings stored in c?
How do you define a function?