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

Which control loop is recommended if you have to execute set of statements for fixed number of times?

824


Give me the code of in-order recursive and non-recursive.

895


What is methods in c?

655


Why does everyone say not to use gets?

614


An instruction which is analysed and acted upon by the processor prior to the compiler going its work a) directive b) constructive c) constant d) absolute mode

617






Explain is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?

684


Differentiate between #include<...> and #include '...'

625


if a is an integer variable, a=5/2; will return a value a) 2.5 b) 3 c) 2 d) 0

1464


I came across some code that puts a (void) cast before each call to printf. Why?

689


Explain how do you print an address?

670


What is the use of a ‘’ character?

595


Difference between malloc() and calloc() function?

666


Explain what is the use of a semicolon (;) at the end of every program statement?

746


What is the use of getchar() function?

639


What are multidimensional arrays?

662