-->





Translate

search a character in a string strchr c++ example

1 comment
strchr is a string class function which helps to find a character in a string. It takes two parameters first is string and second is character to search. 
It returns NULL if specified character not found else it returns a pointer value. Remember it is case sensitive "C" and 'c' are different characters for this function

Write a c++ program which takes a string from user and a character then search a character 



C++ program source code


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

using namespace std;

int main()
{
    char stringObj[100], ch;
    char *result = NULL;

    cout<<"\nEnter a String: ";
    gets(stringObj);
    cout<<"\nEnter character to search in string:   ";
    cin>>ch;
    result = strchr(stringObj, ch);
    if(result == NULL) {
         cout<<"\nCharacter ' "<<ch<<" ' NOT FOUND in : "<<stringObj<<endl;
    } else {
         cout<<"\nCharacter ' "<<ch<<" ' FOUND in : "<<stringObj<<endl;
    }
          return 0;
}


program output: 
strchr c++ example

 Program with case sensitive output
strchr c++ example


See also:



1 comment:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete