Multi Whitespace Compressor

Several times we are forced to operate on strings where there are multiple whitespace characters or various white space characters intermixed. Using regular expressions, it is very simple to replace one or more consecutive whitespace characters by a single space character. For example in Python one could do
>>> import re >>> re.replace(r'\s+', ' ', 'rahul\t agrawal') 'rahul agrawal'
For fun I thought to code a function in C to just do the same. Following is my little code
/*
 * multi_space_compressor
 *
 * compresses multiple white space characters to single
 * white space
 *
 */

#include <assert.h> #include <ctype.h> #include <malloc.h> const unsigned int MOFIFY_ORIGINAL = 1<<0; char *multi_space_compressor(char *src, const unsigned int params) {     int last_char_space = 0;     char cur_char;     char *dst;     int i = 0;     if(!(params && MOFIFY_ORIGINAL))     {         /* do a pass to determine the length and allocate the string */         int  i, req_chars = 0;         for(i=0; (cur_char = *(src + i)); i++)         {             if(!isspace(cur_char))             {                 /* non white space */                 req_chars ++;                 last_char_space = 0;             }             else if (!last_char_space)             {                 req_chars ++;                 last_char_space = 1;             }         }     dst = (char *)malloc(req_chars + 1);     assert(dst != 0);     }     else         dst = src;     last_char_space = 0;     i = 0;     for(;(cur_char = *src);src++)     {         if(!isspace(cur_char))         {             /* non white space */             *(dst + i) = cur_char;             last_char_space = 0;             i++;         }         else if(!last_char_space)         {             *(dst + i) = ' ';             last_char_space = 1;             i++;         }     }     *(dst + i) = '\0';     return dst; }

1 comments:

infogroupsolution said...

many people live their life different way

Post a Comment

top