Wednesday, March 9, 2011

Code: Remove duplicate characters from string

Earlier we have seen code for checking for duplicates. We are now going to extend that code to remove the duplicates from the string.

void RemoveDuplicates(char* pchStringofDuplicates)
{
      if( NULL == pchStringofDuplicates || NULL == (pchStringofDuplicates +1))
            return ;

      char* pchPointToEnd, *pchPointToStart = pchStringofDuplicates++;
      pchPointToEnd = pchStringofDuplicates;

      while(*pchPointToEnd != NULL)
      {
            char* pchComparator = pchPointToStart;
            while(pchComparator != pchPointToEnd)
            {
                  if(*pchComparator == *pchPointToEnd)
                  {
                        break;
                  }
                  pchComparator++;
            }
            if(pchComparator == pchPointToEnd)
            {
                  *pchStringofDuplicates++ = *pchComparator;
            }
            pchPointToEnd++;
      }
      *pchStringofDuplicates = *pchPointToEnd;
}

Considering second character as tail(at first time), we will compare every character from start to tail, with tail. If we don't found match then we will add the tail in the same string. Do this till end of string.

Note: In C++ we are passing the char array to this function. If you try passing char* string you may get Access Violation error.  

Monday, March 7, 2011

Code: Make first character of string to upper case


Here the problem is just simple, we have to make uppercase to the first character of every input string. 


First Method: In this method we are going to consider that the string is having only ASCII characters. Then we will check first if the character fall between a to z, if yes then we will convert it to upper case by just adding the difference as shown below.

void MakeFirstCharUpper(char* pchInputString)
{
int aToInt = (int)'a';
int zToInt = (int)'z';
int AtoInt = (int)'A';


if(pchInputString == NULL)
return ;


int firstChar = (int)*pchInputString;
if(firstChar  <= zToInt && firstChar  >= aToInt )
{
*pchInputString = (char)(AtoInt + (firstChar  - aToInt));
}
}


Second Method: 
This will require when your string contains UNICODE characters. Here we will make copy of the original string, we will make that string to upper case, then we will only copy the first character of upper case string to original string.



void MakeFirstCharUpperUNICODEVersion(char* pchInputString)
{
if( NULL == pchInputString)
return;


if(strlen(pchInputString) == 1)
{
pchInputString = strupr(pchInputString);
return;
}


char* strPartI = new char(strlen(pchInputString));
strncpy(strPartI, pchInputString, strlen(pchInputString));


strPartI = strupr(strPartI);


*pchInputString = *strPartI;
}

Thursday, March 3, 2011

Check if duplicate characters present in string

The problem we are discussing here is you have to return true if string specified contain at least one duplicate character. First we will be trying this without using the additional buffer. 


Idea is just simple: Try comparing each character from second character to end, with every character from start to that character. Don't try to compare from first character, otherwise  you will find the first match at the start itself.




bool IsDuplicatesCharPresent(char* pchStringToVerify)
{
if(pchStringToVerify == NULL)
return false;


char *pchStringStart = pchStringToVerify++;


while(pchStringToVerify != NULL)
{
char* pchComparator = pchStringStart;
while(pchComparator != pchStringToVerify)
{
if(*pchComparator == *pchStringToVerify)
{
return true;
}
pchComparator++;
}
pchStringToVerify++;
}
return false;
}




This can also be done using additional buffer, but your additional buffer may vary using as per the string character set. For example suppose if you are sure of having only ASCII characters then you will require buffer of size 256 length only, as shown below.



bool IsDuplicatesCharPresent(char* pchStringToVerify)
{
if(pchStringToVerify == NULL || (pchStringToVerify + 1) == NULL)
return false;
bool bArray[256] = {false};


while(*pchStringToVerify != NULL)
{
if(true == bArray[ (int)*pchStringToVerify ] )
return true;
else
bArray[ (int)*pchStringToVerify ] = true;
pchStringToVerify++;
}


return false;
}


This code is written considering the string is C style string, i.e. terminating with NULL.