0

I have one textline that says "This is cool" stored in a 2d array called arr. arr[0] would be "This", arr[1] would be "is" and so on. I have another 2d array with user inputted words. I need to compare every word in the userinput array to every word in the arr array to see how often each word in the userinput array occurs in the arr array.

Here is the code for that:

for (int i = 0; i < boo; i++) {
        occurrence = 0;
        for (int j = 0; j < baa; j++) {
            if (*arr[j] == *userinput[i]) {
                occurrence += 1;
            }
            if (j == baa-1) {
                cout << userinput[i] << " occurs " << occurrence << " times \n";
            }
        }
    }

Note that boo is the amount of items in the userinput array, and baa is the amount of items in the arr array

The issue is this program is only comparing the first letter of each word, so the word island will count the same as the word icecream. Any suggestions on how I can compare the entire word rather than just the first letter?

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
itsnewton
  • 1
  • 2
  • I already tried removing the * in the if (*arr[j] == *userinput[i]), that just returns 0 occurrences for every word, no matter if it actually occurs or not. – itsnewton Feb 28 '21 at 23:23
  • https://stackoverflow.com/help/minimal-reproducible-example (self-contained is the key for you), also use proper variable names (boo, baa). Show us what the arr and userinput is in code. You said 2d array, so you are trying to compare a row of data? – Allan Wind Feb 28 '21 at 23:30
  • It sure does, thanks man – itsnewton Feb 28 '21 at 23:32
  • @AllanWind I'll take a look, thanks. New to this lol – itsnewton Feb 28 '21 at 23:33
  • No worries, don't be discouraged. We want to help, you just need to make it easy for us to do so. – Allan Wind Feb 28 '21 at 23:34

0 Answers0