-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharOp.c
50 lines (42 loc) · 941 Bytes
/
charOp.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
/*
charOp.c
Provides function to normalise a string
Written by Benny Hwang Jan 2018
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "charOp.h"
// remove trailing and leading space in a string
char *removeSpace(char *str){
// remove leading space
while(isspace(*str)){
str ++;
}
// if all white space
if(*str == '\0'){
return str;
}
char *end = str + strlen(str) - 1;
while(isspace(*end)){
end --;
}
*(end+1) = '\0';
return str;
}
// Convert a string to all lower case
void toLower(char *str){
int i;
for(i=0; str[i] != '\0'; i++){
str[i] = tolower(str[i]);
}
}
void removeTrailingPunc(char *str){
char *end = str + strlen(str) - 1;
// if the trailing character is either of these, removed it by changing end position
while(*end == '.' || *end == ',' || *end == ';' || *end == '?'){
end --;
}
*(end+1) = '\0';
}