-5

Wrote this function which takes two strings and outputs number of common characters. Can't get it to work with online compiler... Seems logically fine to me.

int commonCharacterCount(std::string s1, std::string s2) {
    int stringCount1[26]{ 0 };
    int stringCount2[26]{ 0 };
    int charGet;
    int total{ 0 };

    for (auto i = 0; i < s1.length(); i++)
    {
        charGet = s1[i];
        stringCount1[charGet - 97] = stringCount1[charGet - 97]++;
    }
    for (auto i = 0; i < s2.length(); i++)
    {
        charGet = s2[i];
        stringCount2[charGet - 97] = stringCount2[charGet - 97]++;
    }
    for (auto i = 0; i < 26; i++)
    {
        total = total + min(stringCount1[i], stringCount2[i]);
    }

    return total;
}
Zachscs
  • 3,353
  • 7
  • 27
  • 52
  • 3
    What do you think `x = x++;` means? – Passer By Oct 03 '17 at 05:23
  • 3
    "Can't get it to work" is not a problem description. `std::string` is undeclared. `stringCount1[charGet - 97] = stringCount1[charGet - 97]++` has undefined behavior: You're modifying the same variable twice at the same time. – melpomene Oct 03 '17 at 05:23
  • 1
    @ZacharySchwatz __DON'T__ fix that line. It is part of your original question – Passer By Oct 03 '17 at 05:28
  • 4
    ... Huh? Why do you think I'm upset? – melpomene Oct 03 '17 at 05:30
  • Possible duplicate of [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – melpomene Oct 03 '17 at 05:35
  • @ZacharySchwatz No, the title of your question is that the output is 0. This code snippet produces no output. Also, I don't read titles. – melpomene Oct 03 '17 at 05:35
  • @ZacharySchwatz You should read [mcve]. Also, the question "does not show any research effort", which is the first listed downvote reason on the button. – melpomene Oct 03 '17 at 05:36
  • @ZacharySchwatz A very basic thing would be to list the sample inputs you tested with, preferably in the form of a `main` function that calls `commonCharacterCount` (this is part of the MCVE thing). Did you step through your code with a debugger? If so, what did you observe? If not, why not? – melpomene Oct 03 '17 at 05:40
  • I said "step through", not just "run and look at the result". Do you know how to use a debugger? Another angle would be to remove all irrelevant code. Try to throw out all code that doesn't contribute to the exact problem at hand. The function doesn't have to do anything useful. In the optimal case you end up with a single statement that doesn't do what you expect, which makes for a much better question. – melpomene Oct 03 '17 at 05:44

1 Answers1

3

x++ return x's initial value

so

stringCount1[charGet - 97] = stringCount1[charGet - 97]++; returns stringCount1[charGet - 97]'s initial value, means 0. or might be undefined depending on your compiler version, in my case it just happened to return the initial value.


Just use stringCount1[charGet - 97]++ or stringCount1[charGet - 97] = stringCount1[charGet - 97] + 1 instead.

Adam Kotwasinski
  • 4,377
  • 3
  • 17
  • 40