In C - check if char exists in char array

I am trying to check if a character belongs to a list / array of invalid characters.

Based on the Python background, I used to just say:

for c in string: if c in invalid_characters: #do stuff, etc 

How to do this with regular C char arrays?

+41
c string arrays char
Jul 01 '09 at 21:47
source share
7 answers

The equivalent C code is as follows:

 #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, i.e. char array with zero completion.

+29
Jul 01 '09 at 21:50
source share

Less well-known, but extremely useful (and standard, since C89 is the value "forever") function in the C library, provide information in a single call. In fact, there are several functions: - embarrassment of wealth. The following are important for this:

7.21.5.3 strcspn function

Summary

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

Description

The strcspn function calculates 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 strpbrk function

Summary

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

Description

The strpbrk function finds 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 character pointer or a null pointer if there is no character from s2 found in s1.

The question asks "for each char in the string ... if it is in the list of invalid characters".

Using these functions, you can write:

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

Or:

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

Which is better depends on what else you want to do. There is also a related function, strspn() , which is sometimes useful (whitelist instead of blacklist).

+37
Jul 01 '09 at 23:07
source share

Assuming your input is a standard zero-terminated C string, you want to use strchr :

 #include <string.h> char* foo = "abcdefghijkl"; if (strchr(foo, 'a') != NULL) { // do stuff } 

If, on the other hand, your array does not end in zero (i.e. just raw data), you will need to use memchr and specify the size:

 #include <string.h> char foo[] = { 'a', 'b', 'c', 'd', 'e' }; // note last element isn't '\0' if (memchr(foo, 'a', sizeof(foo)) { // do stuff } 
+21
Jul 01 '09 at 21:50
source share

use the strchr function when working with C. strings

 const char * strchr ( const char * str, int character ); 

Here is an example of what you want to do.

 /* strchr example */ #include <stdio.h> #include <string.h> int main () { char invalids[] = ".@<>#"; char * pch; pch=strchr(invalids,'s');//is s an invalid character? if (pch!=NULL) { printf ("Invalid character"); } else { printf("Valid character"); } return 0; } 

Use memchr when working with memory blocks (like arrays with zero completion)

 const void * memchr ( const void * ptr, int value, size_t num ); /* memchr example */ #include <stdio.h> #include <string.h> int main () { char * pch; char invalids[] = "@<>#"; pch = (char*) memchr (invalids, 'p', strlen(invalids)); if (pch!=NULL) printf (p is an invalid character); else printf ("p valid character.\n"); return 0; } 

http://www.cplusplus.com/reference/clibrary/cstring/memchr/

http://www.cplusplus.com/reference/clibrary/cstring/strchr/

+4
Jul 01 '09 at 21:49
source share

Do you want to

strchr (const char * s, int c)

If the character c is in string s, it returns a pointer to a location in s. Otherwise, it returns NULL. So just use your list of invalid characters as a string.

+3
Jul 01 '09 at 21:52
source share

strchr to search for char from the beginning ( strrchr from the end):

  char str[] = "This is a sample string"; if (strchr(str, 'h') != NULL) { /* h is in str */ } 
+1
Jul 01 '09 at 21:51
source share

I believe the original question said:

character belongs to list / array invalid characters

and not:

belongs to a zero-terminated string

which, if that were the case, then strchr indeed be the most appropriate answer. If, however, there is no null ending for an array of characters, or if the characters are in the list structure, then you need to either create a string with a terminating zero, or use strchr , or manually strchr over the items in the collection, checking each one in turn. If the collection is small, then a linear search will be fine. A larger collection may require a more appropriate structure to improve search time — a sorted array or balanced binary tree, for example.

Choose what is best for you.

0
Jul 01 '09 at 22:03
source share



All Articles