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?
The less well-known but extremely useful (and standard since C89 — meaning 'forever') functions in the C library provide the information in a single call. Actually, there are multiple functions — an embarrassment of riches. The relevant ones for this are:
The question asks about 'for each char in string ... if it is in list of invalid chars'.
With these functions, you can write:
Or:
Which is better depends on what else you want to do. There is also the related
strspn()
function which is sometimes useful (whitelist instead of blacklist).