-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeptember29.cpp
More file actions
105 lines (84 loc) · 1.9 KB
/
September29.cpp
File metadata and controls
105 lines (84 loc) · 1.9 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
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define FILE1 "start.txt"
#define FILE2 "end.txt"
#define MAXSTRING 256
int countSpaces(char str[])
{
int count = 0;
for(int i = 0; i < strlen(str); i++) if (str[i] == ' ') count++;
return count;
}
int countWords(char name[])
{
FILE* pF;
int count = 0;
if (pF = fopen(name, "r+t"))
{
char str[MAXSTRING];
fgets(str, MAXSTRING, pF) + 1;
while (!feof(pF) && strcmp(str, ""))
{
count += countSpaces(str) + 1;
fgets(str, MAXSTRING, pF);
}
fclose(pF);
}
return count;
}
void removeSpaces(char dst[], char src[])
{
FILE *pF1, *pF2;
if (pF1 = fopen(src, "rt"))
{
char str[MAXSTRING];
fgets(str, MAXSTRING, pF1);
while (!feof(pF1) && strcmp(str, ""))
{
while (str[0] == ' ' && str[1] && str[0])
strcpy(str, str + 1);
for (int i = 0; i < strlen(str); i++)
if(str[i] == ' ' && str[i + 1] == ' ')
strcpy(str + i--, str + i + 1);
if (pF2 = fopen(dst, "at"))
{
fputs(str, pF2);
fclose(pF2);
}
fgets(str, MAXSTRING, pF1);
}
fclose(pF1);
}
}
void writeText(char name[])
{
FILE* pF;
if (pF = fopen(name, "w+t"))
{
char str[MAXSTRING];
printf(">>> ");
fflush(stdin);
gets_s(str);
while(strcmp(str, ""))
{
fputs(str, pF);
fputc('\n', pF);
printf(">>> ");
fflush(stdin);
gets_s(str);
}
fclose(pF);
}
}
int main()
{
writeText(FILE1);
removeSpaces(FILE2, FILE1);
printf("%d\n", countWords(FILE2));
remove(FILE1);
remove(FILE2);
return 0;
}