-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathaddWhiteSpace.c
More file actions
57 lines (48 loc) · 1.08 KB
/
Copy pathaddWhiteSpace.c
File metadata and controls
57 lines (48 loc) · 1.08 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
// them khoang trang vao sau dau cau
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
bool isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
bool specialCharacter(char c) {
return c == ',' || c == '.' || c == '(' || c == ')'
|| c == '?' || c == ':' || c == ';' || c == '!';
}
void addWhiteSpace(char*, char *, int);
int countPos(char*);
int countPos(char* s) {
int counter = 0;
int i;
int len = strlen(s);
for(i = 0; i < len; i++) {
if(specialCharacter(s[i]) && !specialCharacter(s[i + 1])
&& !isWhiteSpace(s[i + 1])) {
counter++;
}
}
return counter;
}
void addWhiteSpace(char *s, char *res, int amount) {
int i, j = 0;
int len = strlen(s);
for(i = 0; i < len; i++) {
res[j++] = s[i];
if(specialCharacter(s[i]) && !specialCharacter(s[i + 1])
&& !isWhiteSpace(s[i + 1])) {
res[j++] = ' ';
}
}
res[len + amount] = '\0';
}
int main(){
char s[100];
fgets(s, 99, stdin);
int num = countPos(s);
int len = strlen(s);
char *p = (char *) malloc(len + num + 1);
addWhiteSpace(s, p, num);
printf("%s", p);
return 0;
}