I have a function that finds the difference between two characters that are in a null terminated array. Here's the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int diff(constchar* c1, constchar* 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?
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".
#include <iostream>
#include <cstdlib>
int diff(constchar* c1, constchar* 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
1 2 3 4 5 6 7 8 9
#include <iostream>
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;
}
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.
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.
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"
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.
// Example program
#include <iostream>
int diff(constchar* c1, constchar* 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.
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?