In C - check if a char exists in a char array

arrays, c, char, string

Solution

The equivalent C code looks like this:

#include <stdio.h>
#include <string.h>

// This code outputs: h is in "This is my test string"
int main(int argc, char* argv[])
{
   const char *invalid_characters = "hz";
   char *mystring = "This is my test string";
   char *c = mystring;
   while (*c)
   {
       if (strchr(invalid_characters, *c))
       {
          printf("%c is in \"%s\"\n", *c, mystring);
       }

       c++;
   }

   return 0;
}

Note that invalid_characters is a C string, ie. a null-terminated `char` array.

Problem

I'm trying to check if a character belongs to a list/array of invalid characters. Coming from a Python background, I used to be able to just say : ``` for c in string: if c in invalid_characters: #do stuff, etc ``` How can I do this with regular C char arrays?

Original source