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


Please Help Members By Posting Answers For Below Questions

Does * p ++ increment p or what it points to?

599


What is printf () in c?

570


What is string length in c?

595


What is define c?

561


Is there a way to compare two structure variables?

606






‘ C’ PROGRAME TO SHOW THE TYPE OF TRANGLE BY ACCEPTING IT’S LENGTH .

2357


Explain how can I manipulate strings of multibyte characters?

767


Is fortran still used today?

591


How can I sort more data than will fit in memory?

615


When should I declare a function?

609


Why is extern used in c?

601


Do you know the use of 'auto' keyword?

648


Which built-in library function can be used to match a patter from the string?

729


What is array in c with example?

603


Why header files are used?

629