Write a program to compare two strings without using the
strcmp() function

Answer Posted / md.ershad.ezaz

int strcompare(char *,char *);
void main()
{
char s1[15],s2[15];
int cmp;
clrscr();
puts("Enter first string");
gets(s1);
puts("Enter second string");
gets(s2);
cmp=strcompare(s1,s2);
if(cmp==0)
puts("strings are equal");
else
puts("strings are not equal");
getch();
}
int strcompare(char *p1,char *p2)
{
while(*p1==*p2)
{
if(*p1=='\0')
return(0);
p1++;
p2++;
}
return(*p1-*p2);
}

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain how do you determine the length of a string value that was stored in a variable?

655


What is meant by initialization and how we initialize a variable?

575


write a program in c language to print your bio-data on the screen by using functions.

6229


How macro execution is faster than function ?

651


Do pointers need to be initialized?

546






When do we get logical errors?

623


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

608


Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ].

622


What is mean by data types in c?

538


Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. z-a:zyx......ba -1-6-:-123456- 1-9-1:123456789987654321 a-R-L:a-R...L a-b-c:abbc

5042


Explain why c is faster than c++?

560


How to find a missed value, if you want to store 100 values in a 99 sized array?

801


differentiate built-in functions and user – defined functions.

607


What are the advantages and disadvantages of a heap?

692


Why shouldn’t I start variable names with underscores?

611