strchr - C string.h

C examples for string.h:strchr

Type

function

From

<cstring> 
<string.h>

Description

Locate first occurrence of character in string

Prototype

const char * strchr ( const char * str, int character );
      char * strchr (       char * str, int character );

Parameters

Parameter Description
str C string.
character Character to be located.

Return Value

A pointer to the first occurrence of character in str. If not found, the function returns a null pointer.

Demo Code


#include <stdio.h>
#include <string.h>

int main ()//  ww  w  . ja v a2  s  .c o m
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);

  pch=strchr(str,'s');

  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}

Related Tutorials