fxlibc/src/libc/string/strchr.c

41 lines
860 B
C
Raw Normal View History

2021-05-09 17:34:00 +02:00
#include <string.h>
2020-09-17 19:27:01 +02:00
/*
** The strchr() function returns a pointer to the first occurrence of the
** character c in the strings.
*/
2020-09-17 19:27:01 +02:00
char *strchr(const char *s1, int c)
{
int i = -1;
while (s1[++i] != '\0' && s1[i] != c) ;
return ((s1[i] == '\0') ? NULL : (void *)&s1[i]);
2020-09-17 19:27:01 +02:00
}
/*
** The strchrnul() function is like strchr() except that if c is not found in
** s, then it returns a pointer to the null byte at the end of s, rather
** than NULL.
*/
2020-09-17 19:27:01 +02:00
char *strchrnul(const char *s1, int c)
{
int i = -1;
while (s1[++i] != '\0' && s1[i] != c) ;
return ((void *)&s1[i]);
2020-09-17 19:27:01 +02:00
}
/*
** The strrchr() function returns a pointer to the last occurrence of the
** character c in the strings.
*/
2020-09-17 19:27:01 +02:00
char *strrchr(const char *s1, int c)
{
void *saved;
saved = NULL;
for (int i = 0; s1[i] != '\0'; i++) {
2020-09-17 19:27:01 +02:00
if (s1[i] == c)
saved = (void *)&s1[i];
}
return (saved);
}