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

What is selection sort in c?

617


What are the two types of structure?

590


What does != Mean in c?

599


code for quick sort?

1630


how do you programme Carrier Sense Multiple Access

1530






write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...

1498


What are the advantages of using Unions?

650


Explain 'far' and 'near' pointers in c.

714


Why can't I perform arithmetic on a void* pointer?

646


What are the advantages of using macro in c language?

600


What’s a signal? Explain what do I use signals for?

620


please explain every phase in the "SDLC" in the dotnet.

2187


What is the code for 3 questions and answer check in VisualBasic.Net?

1700


State two uses of pointers in C?

646


How pointers are declared?

575