Use pure c to remove a space at the beginning and end of a string, and keep only one inside if there are contiguous spaces.
C Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
#include<stdio.h> #include<stdlib.h> #include<string.h>
voidTrimspace (Char*pstr) { if(NULL= = pStr)return;
intLow =0; intHigh =0; intStrLen = StrLen (PSTR);
/**< Remove space in front of String * / while(Pstr[high] = ="'&& High < StrLen) { ++high; }
while(High < StrLen) {
if(Pstr[high] = ="') //read consecutive spaces and compress them into a single space { pstr[low++] ="'; while(Pstr[high] = ="'&& High < StrLen) { ++high; } } Else Read consecutive words { while(Pstr[high]! ="'&& High < StrLen) { pstr[low++] = pstr[high++]; } } } /**< Remove spaces at the end of a string * / if(Pstr[low-1] == "') { Pstr[low-1] = ' + '; } Else { Pstr[low] =' + '; } }
intMain () { intsize =1024x768; Char*pstr = malloc (sizeof(Char) * size); Gets (PSTR); Trimspace (PSTR); printf ('%s ', PSTR); Free (PSTR);
return 0; }
|
c spaces in a compressed string