Write a program to compare two strings without using the
strcmp() function
Answer Posted / abhi
#include<stdio.h>
#include<conio.h>
main()
{
char *s1;
char *s2;
int flag=0;
s1=(char *)malloc(10);
s2=(char *)malloc(10);
scanf("%s",s1);
scanf("%s",s2);
while((*s1!='\0')||(*s2!='\0'))
{
if(*s1!=*s2)
flag=1;
else
{
s1++;
s2++;
}
if(flag)
{
if(*s1<*s2)
flag=-1;
break;
}}
if(*s1=='\0'&&*s2!='\0')
flag=-1;
else if(*s1!='\0'&&*s2=='\0')
flag=1;
else
{}
printf("%d",flag);
getch();
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
What is the general form of function in c?
Define macros.
How can I find out how much free space is available on disk?
while initialization of array why we use a[][2] why not a[2][]...?
Is r written in c?
What is structure in c language?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
When should a type cast not be used?
which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above
What is sizeof int?
What is the value of c?
How many types of operators are there in c?
Why is C language being considered a middle level language?
Function calling procedures? and their differences? Why should one go for Call by Reference?
What is register variable in c language?