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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
| #include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
//Прототипы подпрограмм:
void search_a_word(char *text, char *word, int from, int *begin, int *end);
void swap_words(char *text, int begin, int end);
void print_color(char *text, int begin, int end);
void print_normal(char *text, int begin, int end);
//Главная программа:
void main(void)
{
FILE *f_in;
int no_out, i, l_text, l_w1, l_w2, begin, end, from;
char ch, *text, *w1, *w2, *dir;
clrscr();
textmode(C80);
textcolor(RED);
printf("\nEnter the file name: ");
textcolor(WHITE);
scanf("%s",dir);
f_in=fopen(dir,"rb");
if((f_in=fopen(dir,"rb"))==NULL)
{
textcolor(RED+BLINK);
printf("\nUnable to open the file!");
textcolor(WHITE);
exit(1);
}
//0. Вывод исходного текста
textcolor(RED);
printf("\nInput text:\n");
textcolor(WHITE);
fseek(f_in,0,SEEK_END);
l_text=ftell(f_in);
text=(char *)malloc((l_text+1)*sizeof(char));
fseek(f_in,0,SEEK_SET);
for(i=0;i<l_text;i++)
fscanf(f_in,"%c",&text[i]);
*(text+l_text)='\0';
for(i=0;i<l_text;i++)
printf("%c",text[i]);
textcolor(RED);
//1. Ввод слов для поиска и обмена
printf("\nEnter the words you want to swap");
printf("\nThe 1st word: ");
textcolor(WHITE);
scanf("%s",w1);
l_w1=strlen(w1);
textcolor(RED);
printf("The 2nd word: ");
textcolor(WHITE);
scanf("%s",w2);
l_w2=strlen(w2);
textcolor(RED);
printf("Press any key to search a word");
textcolor(WHITE);
from=0;
begin=0;
no_out=1;
//2. Организация цикла поиска и обмена слов
while(no_out)
{
clrscr();
ch=getch();
//2.1. Поиск слова от позиции from (в результате находятся begin и end)
if(begin>=0)
{
clrscr();
//2.1.1. Вывод текста с вывделенным образцом
print_normal(text,0,begin-1);
print_color(text,begin,end);
print_normal(text,end+1,strlen(text-1));
from=end+1;
ch=getch();
}
else
{
no_out=0;
clrscr();
printf("\nDone!");
ch=getch();
}
}
fclose(f_in);
free(text);
textmode(LASTMODE);
}
//3. Подпрограммы:
//3.1 Подпрограмма поиска слов в тексте:
void search_a_word(char *text, char *word, int from, int *begin, int *end)
{
char *ss;
int i,j,n;
//Поиск начала слова
if(from>strlen(text))
{
*begin=-6;
return;
}
else
{
n=strlen(text)-from;
ss=(char *)malloc((n+1)*sizeof(char));
for(i=0;i<=n;i++)
*(ss+i)=*(text+i+from);
*begin=;
if(begin>=0)
{
*begin=*begin+from;
*end=begin+strlen(w)-1;
}
free(ss);
return;
}
}
//3.2 Подпрограмма обмена слов в тексте:
void swap_words(char *text, char *w1, char *w2, int begin, int end)
{
int i;
}
//3.3 Подпрограмма выделения слов в тексте:
void print_color(char *text, int begin, int end)
{
int i;
textcolor(GREEN);
textbackground(BLACK);
for(i=begin;i<=end;i++)
{
putch('"');
putch(*(text+i));
putch('"');
}
textcolor(WHITE);
textbackground(BLACK);
}
//3.4 Подпрограмма вывода текста в обычном режиме:
void print_normal(char *text, int begin, int end)
{
int i;
textcolor(WHITE);
textbackground(BLACK);
for(i=begin;i<=end;i++)
putch(*(text+i));
} |