strrchr function-checks for characters from behind
Use the strrchr function to check from behind if a character is included. You can use the strrchr function by loading string.h.
#include <string.h> char * strrchr (const char * string, int ch);
If string contains ch, it will search from the back and return the first matching address. If not found, returns NULL.
In C language, there is a promise that string ends with "\ 0". The strrchr function presupposes this convention and searches for a string.
When using the strrchr 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 searching from the back with the strrchr function. Displays the position of the string where the last ":" 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 last ":"
const char * match_ptr = strrchr (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 3
C Language Zemi