Originally Posted by
tibegato
I just don't want to use any built-in functions. Also, want to learn something.
Otherwise, I can just use if (a == b).
No, that way you would be comparing pointers. A pointer is equal to the other point if they point to the same memory location.
Ha! I quickly put together an implementation of strcmp that even I do not have a very clear idea of how it works o.O
Code:
include <stdio.h>
int strcmp(const char *a, const char *b)
{
while(*a++ == *b++)
return *a - *b;
}
int main(int argc, char *argv)
{
char *first = "Carb";
char *second = "Carb";
if (strcmp(first, second) == 0)
{
printf("Equal\n");
}else
{
printf("Not equal\n");
}
return 0;
}
I'm honestly not sure how the the while condition gets evaluated such that the loop comes to an end at the end of the string(s). Butt, it seems to work.
(Probably not the best of answers there could be, but I contributed to this thread too hoping to learn).