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

Answer Posted / fionaa

int compare(char str2[], char str1[]) {
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 {flag = -1;}
return flag;
}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

write a c program to calculate sum of digits till it reduces to a single digit using recursion

2716


When is a null pointer used?

636


What is modeling?

642


What are conditional operators in C?

619


What is the difference between formatted&unformatted i/o functions?

610






What are local variables c?

547


What is the use of pragma in embedded c?

587


What is the heap in c?

637


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

2175


What is #include stdio h and #include conio h?

595


Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..

642


What are the advantages of using macro in c language?

586


Can you please explain the scope of static variables?

597


praagnovation

1774


write a c program to find the sum of five entered numbers using an array named number

1617