This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
strchr
Prototype
#include <string.h>
char *strchr(const char *str, int c);
Description
Finds in the specified string str
, starting from the beginning, the
specified character c
. Returns a pointer to the first character found,
otherwise NULL
.
Implementation
#include <string.h>
char *strchr(const char *str, int c)
{
for (; *str != '\0'; ++str)
if (*str == c)
return (char *)str;
return NULL;
}
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char *valid_opts = "abc";
int c;
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0]) != '\0')
if (strchr(valid_opts, c))
printf("got valid option `-%c'\n", c);
else {
printf("got invalid option `-%c'\n", c);
return EXIT_FAILURE;
}
printf("other arguments: ");
while (argc--)
printf("%s ", *argv++);
printf("\n");
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.11.5.2