views:

152

answers:

2

Hi,

I have some input like this:

"  aaaaa      bbb \n cccccc\n ddddd \neeee   "

And I need to sanitize it like this:

"aaaaa bbb cccccc ddddd neeee"

Basically:

  • Trim all blank spaces at the beginning and end of the string
  • Strip all new lines
  • Strip all spaces when there is more than one, but always leave ONE space between words

Is there any easy way to do this or I'll have to process the string, char by char and copy the appropriate chars to a different variable?

+1  A: 

You could use strtok to lexically tokenize the string, delimit with " \r\n\t". This will make your job easier.

Sean A.O. Harney
Actually, I (personally) think a hand-written loop would be easier. strtok is one of those *ugly* misfeatures :-)
paxdiablo
+1  A: 

Assuming you cannot modify string in place,

void splcpy(char *s, char *m){ //s is the unmodified string
  int word = -1; //keeps track what was stored in last loop
  while(*s){  //until it ends
    if(!isspace(*s)){
      if(word==0)  *m++ = ' '; //if last char was space, add space
      *m++ = *s++;
       word = 1;
    }
    else{
      if(word == 1)   word = 0; //if last char was !space
      while(isspace(*s++)); //consume all space until end
    }
  }
  *m = '\0'; //end the string nicely
}

char *input = "  aaaaa      bbb \n cccccc\n ddddd \neeee   ";
char *modified = malloc(sizeof(char) * strlen(input));

splcpy(input, modified);
N 1.1
Actually my question was if there was a better way than doing just that lol. But I appreciate the code, it's more simple than what I was thinking.
Nazgulled
I applied the code today and just to let others know that the last while (to consume all spaces) it's eating the first letter of each word. I fixed that by incrementing `s` inside the loop.
Nazgulled