When the strings aren't equal, you don't increment the counter, so you keep comparing the same character over and over and over....
There are many other things wrong with this function.
Trying to write my own strcmp function |
Your function returns less information than strcmp, which tells you which string is lexicographically first when they aren't equal. That's a helpful thing to know and it comes practically for free in the code.
I DEFINED MY OWN TRUE AND FALSE AND ALSO USED BOOL TYPEDEF |
Thta's a very bad idea. I'm not even sure that redefining a built-in type is legal.
Line 2: str1counter is misleading since it counts for both strings.
Line 3: I find it helpful to always name boolean variable for the positive sense, so I'd call this
equal
and change the sense of the variable. After all, what does
if (!notequal == false)
mean?
Line 4:
for
loops are your friend. They separate the "loopy" code from the "do each time" code. This should be
for (int counter=0; str1[counter]; ++counter) { ...
Line 7: If you get to the else, then that condition is guaranteed to be true.
Line 8: Once you know that the strings aren't equal, you should return immediately. In particular, you should stop comparing because you may have reached the end of str2, and going beyond could result in a memory error.
Lines 11-15. You could just
return !notequal
or, if using equal like I suggested,
return equal
.