strchr

Returns the address of the first occurrence of a given character in a null-terminated string. The terminating null character is considered to be part of the string.

Format

#include  <string.h>

char *strchr  (const char *str, int character);
Function Variants This function also has variants named _strchr32 and _strchr64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

Arguments

str
A pointer to a null-terminated character string.
character
An object of type int.

Description

See also strrchr in this section.

Return Values
x  The address of the first occurrence of the specified character. 
NULL  Indicates that the character does not occur in the string. 

Example

    #include <stdio.h>
    #include <string.h>
    
    main()
    {
    
    static char s1buf[] = "abcdefghijkl lkjihgfedcba";
    
    int i;
    
    char *status;
    
    /*  This program checks the strchr function by incrementally    *
     *  going through a string that ascends to the middle and then  *
     *  descends towards the end.                                   */
    
        for (i = 0; s1buf[i] != '\0' && s1buf[i] != ' '; i++)
        {
        status = strchr(s1buf, s1buf[i]);
        /* Check for pointer to leftmost character - test 1. */
        if (status != &s1buf[i])
         printf("error in strchr");
        }
    }
    


Previous Page | Next Page | Table of Contents | Index