Wednesday, May 23, 2012

String Manipulation in C - 01

Here is another one...

Problem:
Escape all % characters in a string; % is the escape character.
Example: “I’d like 2% milk” becomes “I’d like 2%% milk”.

I decided to make it work for any problematic character and to be replaced with any escape character that you should choose. Maybe I will create an amendment later that allows you pass an array of characters that each should be escaped.

I took on this problem because they specifically talked about how easy it would be in C++ or Java where string manipulation is MUCH more advanced and defined. In C you can't simply insert the escaped character because it isn't a string but a character array of a fixed size. This means that you are going to have to allocate the space for it.

Here is what I came up with:
#include <stdio.h>
#include <string.h>

// for malloc, in memory. *
#include <stdlib.h>

char *escapeString (char *origString, char badChar, char escapeChar)
{
    int newStringLoc = 0;
    int toEscape = 0; 

    // Determine number of characters we will need to allocate.
    for (int curChar = 0; origString[curChar] != '\0'; curChar++)
        if (origString[curChar] == badChar)
            toEscape++;
   
    char *newString = malloc (strlen(origString) + toEscape);

    // This really shouldn't happen in 2012...
    if (!newString)
        return NULL;
 
    for (int curChar = 0; origString[curChar] != '\0'; curChar++)
    {
        if (origString[curChar] != badChar)
            newString[newStringLoc] = origString[curChar];
        else
        {
            newString[newStringLoc] =  escapeChar;
            newString[++newStringLoc] = origString[curChar];
        }
 
        newStringLoc++; 
   }

   return newString;
}

int main(void)
{
    char *origString = "This is a problematic string! 2%, 3%, 4%%!";
    char *newString = escapeString (origString, '%', '\\');

    // Check to see if it worked..
    printf ("origString: %s\n newString: %s\n", origString, newString);
 
    free (newString);
    return 0;
}

To make it much easier to see what is going on here, I have replaced the original escape character ('%') with ('\'). The output looks like this:


c:\CProgs\Str1-1>lcc strings1.c
c:\CProgs\Str1-1>lcclnk strings1.obj
c:\CProgs\Str1-1>strings1
origString: This is a problematic string! 2%, 3%, 4%%!
 newString: This is a problematic string! 2\%, 3\%, 4\%\%!


for (int curChar = 0; origString[curChar] != '\0'; toEscape += (origString[curChar++] == badChar));

No comments:

Post a Comment