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
Tell me about low level programming languages.
Can a variable be both const and volatile?
Do you know the difference between exit() and _exit() function in c?
Why does not c have an exponentiation operator?
Write a c program to build a heap method using Pointer to function and pointer to structure ?
Should I learn data structures in c or python?
hello freinds next week my interview in reliance,nybody has an idea about it intervew questions..so tell
Who invented bcpl language?
Stimulate calculator using Switch-case-default statement for two numbers
What is the stack in c?
Explain pointers in c programming?
What is the difference between fread buffer() and fwrite buffer()?
What is 1d array in c?
What is pointer and structure in c?
Can an array be an Ivalue?