Checking a char array for invalid characters

I'm attempting to create a function that when passed an array, checks it against an array of valid characters and returns true if they are all valid, or false if just one of them is not.

However my second assert is failing despite there being an invalid character present.

void test (void)
{   
   char testArr1[MAXGRIDHW][MAXGRIDHW] = {{"..."},
                                          {"..."},
                                          {"..."}};

   char testArr2[MAXGRIDHW][MAXGRIDHW] = {{"..z"},
                                          {"..."},
                                          {"..."}};

   assert(noInvalidLetters(testArr1) == true);
   assert(noInvalidLetters(testArr2) == false);

}

bool noInvalidLetters (char b[][MAXGRIDHW])
{
   int i, j;
   bool valid;
   const char validChars[MAXVALIDCHARS] = {'k', 'K', 'r', 'R', 'g', 'G', 'y',
                                           'Y', 'b', 'B', 'm', 'M', 'c', 'C',
                                           'w', 'W', '.'};

   for (i = 0; i < MAXGRIDHW; i++) {
      for (j = 0; j < MAXGRIDHW; j++) {
         if (strchr(validChars, b[i][j])){
            valid = true;
         } else {
            valid = false;
         }
      }
   }
   return valid;
}


Read more here: https://stackoverflow.com/questions/64946577/checking-a-char-array-for-invalid-characters

Content Attribution

This content was originally published by Davospike at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: