IgorZ

IgorZ IgorZ

Niner since 2007

Comments

  • Gary Daniels and Evan Goldring - Mock whiteboard problem

    Hi All!

    how about this
    code does take in account for characters you want to skip,
    but it does not check for case sensivivity

    pure c++, not using any rtl
    avoiding checking for special case if string is "ABA" for example

    TCHAR ptszSkip[] = {' ', ',', '!', '-'};
    bool IsPalindrome(TCHAR* ptsz)
    {
       bool bRet = false;
       if(NULL != ptsz)
       {
          TCHAR* ptszEnd = ptsz;
          while(*ptszEnd != 0)  //get end pointer
          {
             ++ptszEnd;
          }
          --ptszEnd;
          bRet = true;

          while(ptsz < ptszEnd)
          {
             bool bDoCheck = true;
             for(size_t i = 0; i < sizeof(ptszSkip)/sizeof(ptszSkip[0]); i++)
             { /*check for skiped chars*/
                if(*ptsz == ptszSkip[i])
                {
                   ++ptsz;
                   bDoCheck = false;
                   break;
                }
                if(*ptszEnd == ptszSkip[i])
                {
                   --ptszEnd;
                   bDoCheck = false;
                   break;
                }
             }

             if(bDoCheck)
             {
                if(*ptsz != *ptszEnd) //as soon as find first not equal chars ,  return false
                {
                   bRet = false; 
                   break;
                }
                ++ptsz;
                --ptszEnd;
             }
          }
       }
       return bRet;
    }