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

Answer Posted / sandeep a

// Optimize the above soln...
#include<stdio.h>
int str_cmp(const char *s1, const char *s2)
{
unsigned int i = 0, diff;
while(s1[i]!= '\0' || s2[i] != '\0'){
diff = s1[i] - s2[i];
if(!diff)i++;
else break;
}
return diff;
}
int main(int argc, char *argv[1])
{
printf("chuma %d ", str_cmp("abcd","abcde"));
return 0;
}

Is This Answer Correct ?    35 Yes 49 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are called c variables?

569


Who invented b language?

905


Can we increase size of array in c?

531


Program will then find the largest of three numbers using nested if-else statements. User is prompted to enter three numbers. Program will find the largest number and display it on the screen. All three numbers entered by the user are also displayed. If user enters 21, 33, and 5, the output should be as follows: You entered: 21, 33 and 5. The largest number is 33.

1008


What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }

1950






How many keywords are there in c?

583


Explain what is the difference between #include and #include 'file' ?

571


Differentiate between Macro and ordinary definition.

718


Explain how do you list a file’s date and time?

615


Which header file should you include if you are to develop a function which can accept variable number of arguments?

795


What are categories used for in c?

557


What are the types of i/o functions?

671


What is difference between array and pointer in c?

532


What are the keywords in c?

637


What are the basic data types associated with c?

805