Thursday, November 20, 2008

Finding if a character is present in a string

Finding if a character is present in a string
#include "stdafx.h"
#include <>
#include <>
#include <>
using namespace std;
bool CheckCharPresent(_TCHAR *str,_TCHAR ch)
{
if((str == NULL)(ch == NULL))
return false;
_TCHAR *ptr = str;
for(unsigned i = 0; i < _tcslen(str); i++)
{
if(ptr[i] == ch)
return true;
}
return false;
}


int _tmain(int argc, _TCHAR* argv[])
{
bool val = CheckCharPresent(_TEXT("Rajesh"),_TEXT('h'));
val = CheckCharPresent(_TEXT("h"),NULL);
val = CheckCharPresent(NULL,_TEXT('h'));
return 0;
}

No comments: