Previous Page
Next Page

strchr

Search for a given character in a string

#include <string.h>
char *strchr ( const char *s , int c  );

The strchr( ) function returns a pointer to the first occurrence of the character value c in the string addressed by s. If there is no such character in the string, strchr( ) returns a null pointer. If c is a null character ('\0'), then the return value points to the terminator character of the string addressed by s.

Example

typedef struct { char  street[32];
                 char  city[32];
                 char  stateprovince[32];
                 char  zip[16];
               } Address;

char printaddr[128] = "720 S. Michigan Ave.\nChicago, IL 60605\n";
int sublength;
Address *newAddr = calloc( 1, sizeof(Address) );

if ( newAddr != NULL )
{
  sublength = strchr( printaddr, '\n' ) - printaddr;
  strncpy( newAddr->street, printaddr, ( sublength < 31 ? sublength : 31 ) );
  /* ... */
}

See Also

strrchr( ), strpbrk( ), strstr( ); the wide string functions wcschr( ) and wcsrchr( )


Previous Page
Next Page