Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 406
Amarok Avatar asked Jul 01 '09 21:07

Amarok


People also ask

How do you check if a char is in a char array?

The contains() method of Chars Class in Guava library is used to check if a specified value is present in the specified array of char values. The char value to be searched and the char array in which it is to be searched, are both taken as a parameter.

How do I find a character in an array?

binarySearch(char[] a, char key) method searches the specified array of chars for the specified value using the binary search algorithm. The array must be sorted before making this call.

How do you check if a value exists in an array C?

To check if given Array contains a specified element in C programming, iterate over the elements of array, and during each iteration check if this element is equal to the element we are searching for.

Can we use Strcat on char array in C?

Also You can use strncat for one character addition. strcat requires a null-terminated string for its second argument.


2 Answers

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:

7.21.5.3 The strcspn function

Synopsis

#include <string.h> size_t strcspn(const char *s1, const char *s2); 

Description

The strcspn function computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters not from the string pointed to by s2.

Returns

The strcspn function returns the length of the segment.

7.21.5.4 The strpbrk function

Synopsis

#include <string.h> char *strpbrk(const char *s1, const char *s2); 

Description

The strpbrk function locates the first occurrence in the string pointed to by s1 of any character from the string pointed to by s2.

Returns

The strpbrk function returns a pointer to the character, or a null pointer if no character from s2 occurs in s1.

The question asks about 'for each char in string ... if it is in list of invalid chars'.

With these functions, you can write:

size_t len = strlen(test); size_t spn = strcspn(test, "invald");  if (spn != len) { ...there's a problem... } 

Or:

if (strpbrk(test, "invald") != 0) { ...there's a problem... } 

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).

like image 79
Jonathan Leffler Avatar answered Oct 06 '22 14:10

Jonathan Leffler


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.

like image 42
RichieHindle Avatar answered Oct 06 '22 14:10

RichieHindle