Is there any way to check if any or all characters are present in a string in C? -
i trying check , see if give array of characters -
char array_values[] = { 'a','b','c','d','a','b','c','d' };
and running sort of character matching in multiple strings e.g.-
.... str1 = 'aacdbacbaabcacddaaabd' str2 = 'aacbdaabdcaaddcbcaabc' ....
and returning count of each char present in strings.
i know it's done in python, r, perl, wanted figure out in c. maybe regular expression? ideas?
the easiest way in c count each character regardless of presence in array_values
, use array_values
items indexes array of counts results:
int count[256]; (int = 0 ; != 256 ; count[i++] = 0); // example works single string. multiple strings, // iterate on strings source in loop, assigning str // , incrementing counts each of strings. char *str = "aacdbacbaabcacddaaabd"; (char *p = str ; *p ; count[(unsigned char)*p++]++); char array_values[] = { 'a','b','c','d','a','b','c','d' }; (int = 0 ; != 8 ; i++) { printf("found '%c' %d times", array_values[i], count[(unsigned char)array_values[i]]); }
here demo on ideone.
Comments
Post a Comment