Tuesday, September 2, 2008

Palindrome and Trim functions

//Verify if a string is a palindrome or not
bool IsPalindrome(char *pszString)
{
if (NULL == pszString)
return false;

int nStrLen = strlen(pszString);

int ii = 0;
int jj = nStrLen - 1;

for (; ii < jj; ii++, jj--)
{
if (pszString[ii] != pszString[jj])
return false;
}

return true;
}

//This function will remove spaces from a string
void Trim(char *pszString)
{
if (NULL == pszString)
return;

char *Source = pszString;
char *Dest = pszString;

while (*Source)
{
if (*Source == ' ')
{
++Source;
}
else
{
if (Source != Dest)
*Dest = *Source;

++Dest;
++Source;
}
}

*Dest = '\0';
}

0 comments: