/
trim.c
64 lines (55 loc) · 1.49 KB
/
trim.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
** TRIM.C - Remove leading, trailing, & excess embedded spaces
**
** public domain by Bob Stout & Michael Dehlwes
*/
#include <ctype.h>
#include "snip_str.h"
#if defined(__cplusplus) && __cplusplus
extern "C" {
#endif
char *trim (char *str)
{
char *ibuf, *obuf;
if (str)
{
for (ibuf = obuf = str; *ibuf; )
{
while (*ibuf && (isspace (*ibuf)))
ibuf++;
if (*ibuf && (obuf != str))
*(obuf++) = ' ';
while (*ibuf && (!isspace (*ibuf)))
*(obuf++) = *(ibuf++);
}
*obuf = NUL;
}
return (str);
}
#if defined(__cplusplus) && __cplusplus
}
#endif
#ifdef TEST
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
if (argc == 2)
{
printf ("trim(\"%s\")\n", argv[1]);
printf ("returned \"%s\"\n", trim (argv[1]));
}
else
{
fprintf (stderr, "To test this function, call TRIM\n");
fprintf (stderr, "with an argument enclosed in quotes.\n");
fprintf (stderr, " Example:\n");
fprintf (stderr, " C:\\>trim \" test test \"\n");
fprintf (stderr, " trim(\" test test \"\n");
fprintf (stderr, " returned \"test test\"\n\n");
fprintf (stderr, " C:\\>_\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#endif /* TEST */