OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
trimBlanks.c
Go to the documentation of this file.
1 #include <genutils.h>
2 
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdlib.h>
6 
10 void trimBlanks(char* str) {
11 
12  // bail if the string is null
13  if (str == NULL)
14  return;
15 
16  // bail if empty string
17  int i = strlen(str) - 1;
18  if (i < 0)
19  return;
20 
21  // find last non-blank char
22  while (i >= 0) {
23  if (isspace(str[i]))
24  i--;
25  else
26  break;
27  }
28  int length = i + 1;
29 
30  // get rid of beginning spaces
31  i = 0;
32  while (i < length) {
33  if (isspace(str[i]))
34  i++;
35  else
36  break;
37  }
38 
39  if (i > 0) {
40  int j = 0;
41  while (i < length) {
42  str[j++] = str[i++];
43  }
44  // NULL terminate the string
45  str[j] = 0;
46 
47  } else {
48  // NULL terminate the string
49  str[length] = 0;
50  }
51 
52 }
53 
58 char* trimBlanksDup(const char* str) {
59  // bail if the string is null
60  if (str == NULL)
61  return strdup("");
62 
63  // bail if empty string
64  int i = strlen(str) - 1;
65  if (i < 0)
66  return strdup("");
67 
68  // find last non-blank char
69  while (i >= 0) {
70  if (isspace(str[i]))
71  i--;
72  else
73  break;
74  }
75  int length = i + 1;
76 
77  // get rid of beginning spaces
78  i = 0;
79  while (i < length) {
80  if (isspace(str[i]))
81  i++;
82  else
83  break;
84  }
85 
86  char* str2 = (char*) malloc(length - i + 1);
87 
88  int j = 0;
89  while (i < length) {
90  str2[j++] = str[i++];
91  }
92  // NULL terminate the string
93  str2[j] = 0;
94 
95  return str2;
96 }
97 
int j
Definition: decode_rs.h:73
#define NULL
Definition: decode_rs.h:63
char * strdup(const char *)
char * trimBlanksDup(const char *str)
Definition: trimBlanks.c:58
#define isspace(c)
const char * str
Definition: l1c_msi.cpp:35
void trimBlanks(char *str)
Definition: trimBlanks.c:10
int i
Definition: decode_rs.h:71