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
Why is event driven programming or procedural programming, better within specific scenario?
What is a c token and types of c tokens?
What are the string functions? List some string functions available in c.
Explain Function Pointer?
What does %p mean?
general for is %wd,f-d; in this system "w" means a) 'w' represent total width of digits b) 'w' represent width which includes the digits before,after decimal place and the decimal point c) 'w' represent width which includes the digits before only d) 'w' represent width after decimal place only
Why is c not oop?
A variable that is defined in a specified portion of a program but can be used throughout the program a) global variable b) local variable c) character d) none
Is file a keyword in c?
my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?
How do you sort filenames in a directory?
What are the disadvantages of c language?
Why do we use pointer to pointer in c?
Why ca not I do something like this?
What is structure data type in c?