-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeblank.c
39 lines (33 loc) · 1.18 KB
/
deblank.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "deblank.h"
/*----------------------------------------------------------------------*/
/* DEBLANK: Remove white-space characters from the start and end of
a string.
inputs: pointer to null-terminated string
outputs: modifies the input string
returns: value of input pointer
Trailing and leading white space is removed from the string
and the resulting string is shifted so that the first non white
space character is now at the start of the string.
If a null pointer is passed in, nothing happens.
*/
char *deblank(char *str) {
/* local variables */
int i;
char *cptr;
if (str == NULL) return str;
/* Find the last non white space character and terminate the
string there. */
cptr = strchr(str,'\0');
while (--cptr-str>=0 && isspace(*cptr)) ;
*++cptr = '\0';
/* Find the first non white space character. The null character is
not white space so we need not worry about going past the end. */
cptr = str-1;
while (isspace(*++cptr)) ;
i = 0;
while ((*(str+i++) = *cptr++) != '\0') ;
return str;
}