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
What are the types of arrays in c?
Explain the properties of union.
how many key words availabel in c a) 28 b) 31 c) 32
Write the Program to reverse a string using pointers.
What is double pointer?
What is the difference between NULL and NUL?
Explain is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?
What is self-referential structure in c programming?
Why is c called c?
Declare the structure which contains the following members and write in C list of all students who score more than 75 marks. Roll No, Name, Father Name, Age, City, Marks.
What is the return type of sizeof?
What is the best way to store flag values in a program?
Difference between goto, long jmp() and setjmp()?
What is c basic?
difference between object file and executable file