-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.c
126 lines (109 loc) · 2.95 KB
/
C.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
const size_t MAX_LINE = 2048;
const int MAX_SWAP_PER_WORD = 2;
const float SWAP_PROBABILITY = 0.2;
const float MAX_REMOVE_PER_WORD = 0.2;
const float MAX_REMOVE_VOCAL = 0.05;
const float MAX_REMOVE_CONSONANT = 0.10;
const float MAX_REMOVE_DOUBLE = 0.40;
const float H_PROBABILITY = 0.3;
bool rnd(float probability);
void swap(char*a, char*b);
bool isvocal(char c);
void process(char* str);
void capitalize(char* str, size_t len);
void swap_letters(char* str, size_t len);
size_t remove_letters(char* str, size_t len);
size_t add_h(char* str, size_t len);
int main(int argc, char* argv[]) {
if (argc > 1)
freopen(argv[1], "r", stdin);
char line[MAX_LINE];
while (fgets(line, MAX_LINE, stdin) != NULL) {
process(line);
printf("%s", line);
}
return EXIT_SUCCESS;
}
bool rnd(float probability) {
return (float)(rand()) / RAND_MAX < probability;
}
void process(char* str) {
size_t len = strlen(str);
capitalize(str, len);
swap_letters(str, len);
len = remove_letters(str, len);
len = add_h(str, len);
}
void capitalize(char* str, size_t len) {
int i;
for (i = 0; i < len; i++)
str[i] = toupper(str[i]);
}
void swap(char*a, char*b) {
char t = *a;
*a = *b;
*b = t;
}
void swap_letters(char* str, size_t len) {
int i;
int perWord = 0;
for (i = 0; i < len-1; i++) {
char c = str[i];
if (isspace(c)) { perWord = 0; continue; }
if (isalpha(c) && isalpha(str[i+1]) && perWord < MAX_SWAP_PER_WORD && rnd(SWAP_PROBABILITY)) {
swap(&str[i], &str[i+1]);
perWord++;
}
}
}
bool isvocal(char c) {
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
return true;
return false;
}
size_t remove_letters(char* str, size_t len) {
int pos = 0;
int perWord = 0;
int wordLength = 0;
int i;
for (i = 0; i < len; i++) {
char c = str[i];
str[pos] = c;
if (isspace(c) || i == 0) {
perWord = wordLength = 0;
int j;
for (j = i?i+1:0; j < len && isalpha(str[j]); j++)
wordLength++;
}
bool skip = false;
if (isvocal(c) && rnd(MAX_REMOVE_VOCAL))
skip = true;
else if (c == str[i+1] && rnd(MAX_REMOVE_DOUBLE))
skip = true;
else if (isalpha(c) && !isvocal(c) && rnd(MAX_REMOVE_CONSONANT))
skip = true;
if (!skip || perWord >= wordLength*MAX_REMOVE_PER_WORD)
pos++;
else if (skip)
perWord++, wordLength--;
}
str[pos] = 0;
return pos;
}
size_t add_h(char* str, size_t len) {
if (rnd(H_PROBABILITY) && len + 4 < MAX_LINE) {
len--;
str[len++] = ' ';
str[len++] = 'H';
str[len++] = '!';
str[len++] = '?';
str[len++] = '\n';
str[len] = '\0';
}
return len-1;
}