-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelpers.c
More file actions
63 lines (53 loc) · 1.32 KB
/
StringHelpers.c
File metadata and controls
63 lines (53 loc) · 1.32 KB
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
/* ========================================
*
* Copyright 2016 John Harkins and Li He
* All Rights Reserved
*
* ========================================
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "StringHelpers.h"
/*******************************************************************************
* Function Name: TrimString
********************************************************************************
*
* Summary
* Removes leading and trailing whitespace from a string str
*
* Parameters:
* str - the string to be trimmed
*
* Return:
* None.
*
*******************************************************************************/
void TrimString(char *str)
{
int i;
// trim leading whitespace
for (i = 0; str[i] != '\0'; i++) {
if (!isspace(str[i])) {
break;
}
}
// copy string w/o leading whitespace
// (don't read beyond the end of the input buffer)
memmove(str, str + i, strlen(str) + 1 - i);
// trim trailing whitespace
i = strlen(str) - 1; // index of last character
// Edge case: string is empty
if (i == -1) {
str[0] = '\0';
return;
}
do {
if (!isspace(str[i])) {
break;
}
i--;
} while (i >= 0);
str[i + 1] = '\0';
}
/* [] END OF FILE */