Answer to Question #17765 in C++ for apoorva

Question #17765
Write a c++ function that compares two strings and returns 0 if the two strings are equal and -1 if the strings are unequal.
1
Expert's answer
2012-11-02T13:02:56-0400

#include<stdio.h>

int compare_string(char*, char*);

int main()
{
char first[100], second[100], result;

printf("Enter first string
");
gets(first);

printf("Enter second string
");
gets(second);

result = compare_string(first, second);

if ( result == 0 )
printf("Both strings are same.
");
else
printf("Entered strings are not equal.
");

int k;
scanf("%d",&k);
return 0;
}

int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '' || *second == '' )
break;

first++;
second++;
}
if( *first == '' && *second == '' )
return 0;
else
return -1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS