-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim.h
76 lines (75 loc) · 1.33 KB
/
trim.h
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
void trim()
{
char str[10000];
strcpy(str,s);
int n = strlen(str);
int i = 0, j = -1;
int spaceFound = 0;
while (++j < n && str[j] == ' ');
while (j < n)
{
if (str[j] != ' ')
{
if ((str[j] == ',' ||
str[j] == '?') && i - 1 >= 0 &&
str[i - 1] == ' ')
str[i - 1] = str[j++];
else
str[i++] = str[j++];
spaceFound = 0;
}
else if (str[j++] == ' ')
{
if (spaceFound==0)
{
str[i++] = ' ';
spaceFound = 1;
}
}
}
char temp_str[10000]="";
if (i <= 1)
strncpy(temp_str,str,i+1);
else
strncpy(temp_str,str,i);
strcpy(s,temp_str);
strcpy(s_save,temp_str);
}
void trim_r(char * string)
{
char str[10000];
strcpy(str,string);
int n = strlen(str);
int i = 0, j = -1;
int spaceFound = 0;
while (++j < n && str[j] == ' ');
while (j < n)
{
if (str[j] != ' ')
{
if ((str[j] == ',' ||
str[j] == '?') && i - 1 >= 0 &&
str[i - 1] == ' ')
str[i - 1] = str[j++];
else
str[i++] = str[j++];
spaceFound = 0;
}
else if (str[j++] == ' ')
{
if (spaceFound==0)
{
str[i++] = ' ';
spaceFound = 1;
}
}
}
char temp_str[10000]="";
if (i <= 1)
strncpy(temp_str,str,i+1);
else
strncpy(temp_str,str,i);
while(temp_str[strlen(temp_str)-1]==' ')
temp_str[strlen(temp_str)-1]='\0';
strcpy(string,temp_str);
}