2

the problem is this warning at 15 and 18 warning: array subscript has type ‘char’ [-Wchar-subscripts]

deal with Sample Input: They are students. aeiou Sample Output: Thy r stdnts.

#include <cstdio>
#include <cstring>
const int MAXN = 10005;
char str1[MAXN], str2[MAXN];
bool HashTable[128] = {false};
//use HashTable to record the item need to minus

int main()
{
    fgets(str1, sizeof(str1), stdin);
    fgets(str2, sizeof(str2), stdin);
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    for (int i = 0; i < len2; i++) {
        HashTable[str2[i]] = true;
    }
    for (int i = 0; i < len1; i++) {
        if (HashTable[str1[i]] == false) {
            printf("%c", str1[i]);
        }
    }
    return 0;
}

I can run it, but the warning I have no idea.

here

ZealYoung
  • 39
  • 1
  • 5

1 Answers1

1

Casting the char to int or promoting it, e.g., with the unary plus would rid you of the warning.

Note that if the user can enter anything they want (and they usually can) you're risking an out-of-bound access as you could get either a value larger than 127 if char is unsigned or a negative value if char is signed.

The safest strategy would be to expand the array to size 256 and cast the char to unsigned char before using it as an index.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142