strchr function-check if characters are included
Use the strchr function to check if a character is included in the string. You can use the strchr function by loading string.h.
#include <string.h> char * strchr (const char * string, int ch);
If string contains ch, returns the first matched address. If not found, returns NULL.
In C language, there is a promise that string ends with "\ 0". The strchr function presupposes this convention and searches for a string.
When using the strchr function, make sure that the string of the first argument ends with "\ 0".
Get the position where the character was found
This is a sample to get the position where the character is found by the strchr function. Displays the position of the string where the first ":" is found.
#include <string.h> #include <stdint.h> #include <stdio.h> int main (void) { const char * module_name = "Foo::Bar::Baz"; // Get the address of the first ":" const char * match_ptr = strchr (module_name,':'); if (match_ptr! = NULL) { // Calculate the found position (relative position) int32_t match_index = match_ptr --module_name; printf("Match Position%d\n", match_index); } else { printf("Not Match\n"); } }
This is the output result.
Match Position 9