-
Notifications
You must be signed in to change notification settings - Fork 44
/
bai5.6b.c
137 lines (124 loc) · 2.54 KB
/
bai5.6b.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
// chuan hoa xau ki tu
/*
* xoa cac ki tu khoang trang thua o cuoi xau
* xoa ki tu khoang trang thua o dau xau
* xoa ki tu khoang trang thua o giua xau
* them dau cach vao sau dau cau
* viet hoa ki tu dau cau va sau cac dau cau thich hop
*/
#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 == '!';
}
bool capitalSignal(char c) {
return c == '.' || c == '!' || c == '?';
}
void addWhiteSpace(char*, char *, int);
int countPos(char*);
void removeAtTail(char*);
void removeAtHead(char*);
void removeAtMid(char*);
void removeAtTail(char *s) {
int len = strlen(s);
int i = len-1;
while(isWhiteSpace(s[i])) {
s[i] = '\0';
i--;
}
}
void removeAtHead(char *s) {
int counter = 0;
int i;
int len = strlen(s);
// dem xem co tat ca bao nhieu khoang trang
for(i = 0; i < len; i++) {
if(isWhiteSpace(s[i])) {
counter++;
} else {
break;
}
}
// xoa khoang trang
for(i = 0; i <= len - counter; i++) {
s[i] = s[i + counter];
}
}
void removeAtMid(char *s) {
int i, j;
int len = strlen(s);
for(i = 0; i < len; i++) {
if(isWhiteSpace(s[i]) && isWhiteSpace(s[i + 1])) {
for(j = i + 1; j < len; j++) {
s[j] = s[j + 1];
}
i--;
len--;
} else if(isWhiteSpace(s[i]) && specialCharacter(s[i + 1])) {
for(j = i; j < len; j++) {
s[j] = s[j + 1];
}
i--;
len--;
} else if(s[i] == '\t') {
s[i] = ' ';
}
}
}
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';
}
void capitalize(char* s) {
int i;
int len = strlen(s);
if(len > 0) {
s[i] = toupper(s[i]);
}
for(i = 0; i < len; i++) {
if(capitalSignal(s[i])) {
s[i + 2] = toupper(s[i + 2]);
}
}
// hello. world!
}
int main(){
char s[1000];
fgets(s, 999, stdin);
removeAtTail(s);
removeAtHead(s);
removeAtMid(s);
int num = countPos(s);
int len = strlen(s);
char *p = (char *) malloc(len + num + 1);
addWhiteSpace(s, p, num);
capitalize(p);
printf("%s", p);
return 0;
}