strchr
Header: <string.h>
- Finds the first occurrence of ch (after conversion to char as if by (char)ch) in the null-terminated byte string pointed to by str (each character interpreted as unsigned char). The terminating null character is considered to be a part of the string and can be found when searching for ‘\0’.
# Declarations
char* strchr( const char* str, int ch );
/*QChar*/ *strchr( /*QChar*/ *str, int ch );
(since C23)
# Parameters
str: pointer to the null-terminated byte string to be analyzedch: character to search for
# Return value
Pointer to the found character in str, or null pointer if no such character is found.
# Example
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str = "Try not. Do, or do not. There is no try.";
char target = 'T';
const char* result = str;
while((result = strchr(result, target)) != NULL)
{
printf("Found '%c' starting at '%s'\n", target, result);
++result; // Increment result, otherwise we'll find target at the same location
}
}