### Having trouble with looping over two const char*

I have a function that finds the difference between two characters that are in a null terminated array. Here's the function:

 ``12345678910111213141516171819`` ``````int diff(const char* c1, const char* c2) { for (int i = 0; i < 5; ++i) // i=5 value is just a placeholder { if (tolower(c1[i]) < tolower(c2[i])) { return -1; } if (tolower(c1[i]) > tolower(c2[i])) { return +1; } if (tolower(c1[i]) == tolower(c2[i])) { return 0; } } return 0; }``````

The function takes two command line arguments like "abc abz" etc and it's supposed to loop over each characters in order and find the difference (suppose the difference between a and z would be 25). But in this case it only finds the difference between the characters in the first position. What's wrong?
Last edited on
 I have a function that finds the difference between two characters that are in a null terminated array.
 it's supposed to loop over each characters in order and find the difference.

It's doing exactly what you're telling it to :)

Your loop's declaration is correct, but you are returning either -1, 1, or 0 in the first iteration, no matter what.
i = 1 is never reached because every logic path returns.

I'm not even sure what you mean when you say "find the difference" for more than one character.
What should `diff("abcde", "abbbb")` return? 3?
If so, you're finding what's known as the Hamming distance between two strings.

Otherwise, you should be more clear in what you mean by "difference".

 ``1234567891011121314151617181920`` ``````#include #include int diff(const char* c1, const char* c2) { int total_diff = 0; for (int i = 0; i < 5; ++i) // i=5 value is just a placeholder { if (tolower(c1[i]) != tolower(c2[i])) { total_diff++; } } return total_diff; } int main() { std::cout << diff("abcde", "abbbb") << std::endl; }``````

Also, finding the difference of two characters would look like this
 ``123456789`` ``````#include int individual_diff(char a, char b) { return std::max(a, b) - std::min(a, b); } int main() { std::cout << individual_diff('c', 'z') << std::endl; }``````
Last edited on
By difference I meant in the order of the letters, sorry. If the difference between the first and the second letter is positive then it returns +1 and if negative it returns -1. For example, the correct answer to the command abc and abz would be -1 because z comes 23 letters after c.
Oh, that makes more sense. So you're try to give their alphabetical ordering.

If the letters equal each other, then don't return. Instead, you have to look at the next letter and compare them

 ``12345678910111213141516171819202122232425262728`` ``````// Example program #include int diff(const char* c1, const char* c2) { for (int i = 0; i < 5; ++i) // i=5 value is just a placeholder { if (tolower(c1[i]) < tolower(c2[i])) { return -1; } if (tolower(c1[i]) > tolower(c2[i])) { return +1; } } return 0; } int main() { std::cout << diff("apple", "banan") << std::endl; std::cout << diff("banan", "apple") << std::endl; std::cout << diff("mango", "mingo") << std::endl; std::cout << diff("brick", "brick") << std::endl; } ``````

BTW the functionality you're looking for already exists in the standard library! In fact, since C.

 ``123456789`` ``````#include #include int main() { std::cout << strcmp("apple", "banan") << std::endl; std::cout << strcmp("banan", "apple") << std::endl; std::cout << strcmp("fruit", "fruit") << std::endl; }``````

http://www.cplusplus.com/reference/cstring/strcmp/
Last edited on
Oh, I get it now, really appreciate your time on this one!
I do know about strcmp as well, but apparently we are supposed to use this method instead.

Also, I am having on more issue with a different part of the assignment. If you could help then that would be awesome!

Thank you!
Is there any way I could implement a 0 return value in case the two strings entered in are the same? That's what my original function had but it exited the loop when I returned 0.
Does that not happen already? What does diff("fruit", fruit") return for you?
Last edited on
In the first program you posted, the loop just exits when it gets to the brick statement. There is no "0" returned.
Last edited on
Still don't understand. My first post was finding the Hamming distance, which you then explained isn't what you wanted. Then I posted the program that did the same behavior as strcmp. You want to look at http://www.cplusplus.com/forum/beginner/229302/#msg1039874

By
 If the letters equal each other, then don't return
I should have said,
"If the letters equal each other, then don't return immediately in that iteration"

The output of that program should be
 ``1234`` ``````-1 1 -1 0``````
Last edited on
I do get that output when I try it in visual studio, but when I try the same code in unix and pass in the argument ` brick brick ` I get -1 for some reason, not 0. I don't understand how the outputs could differ.
If you're copying my code directly, the code I posted doesn't take in any command-line arguments.

If you want to use command-line arguments, you'd do something like this

 ``12345678910111213141516171819202122232425262728`` ``````// Example program #include int diff(const char* c1, const char* c2) { for (int i = 0; i < 5; ++i) // i=5 value is just a placeholder { if (tolower(c1[i]) < tolower(c2[i])) { return -1; } if (tolower(c1[i]) > tolower(c2[i])) { return +1; } } return 0; } int main(int argc, char* argv[]) { if (argc < 3) return 1; // not enough arguments std::cout << diff(argv[1], argv[2]) << std::endl; return 0; }``````

If you're stilll having problems, post your actual code and we can take a look.
Last edited on
Unfortunately I am not supposed edit the main function. I am only supposed to work on the diff function. Is there any other way I can get the 0 output using cmd line args with only the diff function?
Those are just examples that you can then apply. Show us your main function, and we can [try to] help you work around it.