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

Answer Posted / fionaa

corrected:

Returns an integral value indicating the relationship
between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character
that does not match has a greater value in str1 than in
str2; And a value less than zero indicates the opposite.

int compare(char str1[], char str2[]) {
int flag = -1;
int i=0;
while(str1[i]!='\0' && str2[i]!='\0'){
if((str1[i]==str2[i])) {flag = 0;}
else if (str1[i]>str2[i]) {
flag=1;
break;
}else if(str1[i]<str2[i]){
flag = -1;
break;
}
i++;
}

if(strlen(str1)==strlen(str2) && flag==0 ){
flag = 0;
}
else if(strlen(str1)>strlen(str2) && flag==0 ){
flag = 1;
}
else if(strlen(str1)<strlen(str2) && flag==0 ){flag = -1;}
return flag;

}

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Tell me about low level programming languages.

631


Can a variable be both const and volatile?

661


Do you know the difference between exit() and _exit() function in c?

601


Why does not c have an exponentiation operator?

620


Write a c program to build a heap method using Pointer to function and pointer to structure ?

4165






Should I learn data structures in c or python?

567


hello freinds next week my interview in reliance,nybody has an idea about it intervew questions..so tell

1654


Who invented bcpl language?

695


Stimulate calculator using Switch-case-default statement for two numbers

2435


What is the stack in c?

709


Explain pointers in c programming?

623


What is the difference between fread buffer() and fwrite buffer()?

662


What is 1d array in c?

590


What is pointer and structure in c?

556


Can an array be an Ivalue?

657