search a character in a string strchr c++ example

search a character in a string strchr c++ example
Di Posting Oleh : Simple Learning
Kategori : function strchr string

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:



0 Response to "search a character in a string strchr c++ example"

Post a Comment