I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCString.h

strchr

Locate character in string
+ View other versions (3)

Interface

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

Description

The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered part of the string; therefore if c is '\0', the functions locate the terminating '\0'.

The strrchr function is identical to strchr except it locates the last occurrence of c.

Example:

Example - Locate character in string
Workings
#include <stdio.h>
#include <string.h>
int main()
{
  // define some string
  char s[10] = "abcdefgh";
  // look for the character 'e' in the string s
  char *pos = strchr(s, 'e');
  // display search result
  if (pos)
    printf("Character 'e' found at position %d.\n", pos - s);
  else
    printf("Character 'e' not found.\n");
  return 0;
}
Solution
Output:
Character 'e' found at position 4.

Return Values

The functions strchr and strrchr return a pointer to the located character, or NULL if the character does not appear in the string.

Standards

The functions strchr and strrchr conform to ISO/IEC 9899:1990 ("ISO C90").