strchr

Overview :

The "strchr" is used for scaning the string for the first occurence of a given character.

Syntax :
char *strchr(const char *s,char c);
Description :

The "strchr" function accepts 2 arguments

  • char *s -> string


  • c -> character to be located in string "s"


  • The strchr() function returns a pointer to the first occurrence of the character "c" in the string "s".


  • The pointer moves in the string till NULL character, searching for the given input character.


  • The NULL-terminator is considered to be part of a string.

The 'strchr' returns a pointer to the first occurrence of the character matched in string "s".

The function returns NULL if the characer is not found in the string.

Example :
#include<iostream>
using namespace std;
#include<string.h>

int main()
{
  char str[10];
  char *ptr,k='c';

  cout<<"enter string ";
  cin>>str;

   ptr=strchr(str,k);
    if(ptr)
      cout<<"the character "<<k<<" is at position "<<ptr-str;
    else
      cout<<"the character was not found ";
   return 0;
  }

Comments

comments powered by Disqus