-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim-leading-white-space.c
141 lines (99 loc) · 3.05 KB
/
trim-leading-white-space.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
CODEFORWIN
STRING EXERCISES
TRIM LEADING WHITE SPACES FROM A STRING
coded by Fatih Cinar
on March 26th, 2020
*/
#include <stdio.h>
#define MAX_SIZE 350
enum CharacterInformation
{
CHR_STRING_TERMINATION = '\0',
JUST_SPACE = ' ',
NEW_LINE = '\n',
TAB = '\t'
};
int isWhiteSpace(char chr){
/*
this function figures out whether the given character
is a white space or not
*/
enum Information
{
WHITE_SPACE = 1,
NOT_WHITE_SPACE = 0
};
switch(chr){
case TAB:
case NEW_LINE:
case JUST_SPACE:
return WHITE_SPACE;
default:
return NOT_WHITE_SPACE;
}
}
void trimLeadingWhiteSpaces(char* string){
/*
this function removes all the leading white space
characters from a character array
only works with dynamically allocated memory
*/
if(!isWhiteSpace(string[0])){
// if the first character is not a white space
// do not waste resources doing array operations
return;
}
else{
// if there is white space for sure
// now do the operations
int indexFirstNotWhiteSpace;
// index of the first not white space character
int itr = 0; // iterator to iterate on string
// get the index of the first non-white-space character
while(isWhiteSpace(string[itr])){
itr++;
}
indexFirstNotWhiteSpace = itr;
// and shift all the character till the end to the beginning of the char[]
int forward = indexFirstNotWhiteSpace;
// indeces for the forward characters that are not white space
int beginning = 0; // for pointing to the beginning indeces
for( ;string[forward] != CHR_STRING_TERMINATION; forward++, beginning++){
string[beginning] = string[forward];
// the non-white-space characters at the later parts of the string
// will be shifted to the beginning of the list
}
int newEnd = beginning;
// now beginning show the new end of the string
string[newEnd] = CHR_STRING_TERMINATION;
return;
}
}
void testFunctionality(){
/*
this function tests the functinality of the function trimLeadingWhiteSpaces()
*/
/* TEST 1 */
printf("TEST 1");
char string1[MAX_SIZE] = " this is the string";
printf("Before Trimmed: %s\n",string1);
trimLeadingWhiteSpaces(string1);
printf("After Trimmed: %s\n", string1);
/* TEST 2 */
printf("TEST 2");
char string2[MAX_SIZE] = "\n\n\n \t\t \n\t are we happy";
printf("Before Trimmed: %s\n", string2);
trimLeadingWhiteSpaces(string2);
printf("After Trimmed: %s\n", string2);
/* TEST 3 */
printf("TEST 3");
char string3[MAX_SIZE] = "just that man";
printf("Before Trimmed: %s\n", string3);
trimLeadingWhiteSpaces(string3);
printf("After Trimmed: %s\n", string3);
}
int main(){
testFunctionality();
return 0;
}