Breaking News
Loading...
Friday, 30 October 2015

search a character in a string strchr c++ example

12:33
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 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
Toggle Footer