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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORDS_MAX 30
#define WORD_LEN 11
#define WORD_MIN_LEN 2
#define WORD_MASK "%10s"
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
int main(void){
FILE * f;
char words[WORDS_MAX][WORD_LEN], * p;
int i, count, len;
if ( ! ( f = fopen(INPUT_FILE, "r") ) ){
perror("fopen");
exit(1);
}
for ( count = 0; count < WORDS_MAX && fscanf(f, WORD_MASK, words[count]) == 1; ++count ){
if ( strlen(words[count]) < WORD_MIN_LEN ){
fprintf(stderr, "Wrong content in input file!\n");
exit(1);
}
if ( p = strrchr(words[count], '.') ){
*p = '\0';
++count;
break;
}
}
if ( ferror(f) || fclose(f) ){
perror("FILE");
exit(1);
}
if ( count < 2 ){
fprintf(stderr, "Only one word found in input file!\n");
exit(1);
}
if ( ! ( f = fopen(OUTPUT_FILE, "w") ) ){
perror("fopen");
exit(1);
}
for ( i = 0; i < count - 1; ++i ){
if ( strcmp(words[i], words[count-1]) ){
if ( ( len = strlen(words[i]) ) & 1 ){
p = words[i] + len / 2;
memmove(p, p + 1, strlen(p));
}
if ( fprintf(f, "%s ", words[i]) < 0 ){
perror("fprintf");
exit(1);
}
}
}
if ( fclose(f) ){
perror("FILE");
exit(1);
}
fprintf(stderr, "Done.\n");
exit(0);
} |