/
vsub.c
195 lines (177 loc) · 4.99 KB
/
vsub.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// debug_flag = 1 => debugging code is enabled
// debug_flag = 0 => debugging code is disabled
int debug_flag = 0;
int main()
{
void display_help();
void get_strings();
char *search_strings();
void copy_strings();
void display_results();
char str1[32], str2[4], str3[8], str4[1024];
char *ptr1, *ptr2, *ptr3;
char *start_ptr, *match_ptr, *copy_ptr;
int match_ctr;
ptr1 = ptr2 = ptr3 = NULL;
start_ptr = match_ptr = copy_ptr = NULL;
match_ctr = 0;
display_help();
get_strings( str1, str2, str3 );
ptr1 = str1;
ptr2 = str2;
ptr3 = str3;
start_ptr = str1;
copy_ptr = str4;
// TO DO: while( *start_ptr != '\0' )
while( start_ptr <= ( str1 + strlen( str1 ) ) )
{
if( debug_flag == 1 )
printf( "\n START ptr = %c", *start_ptr );
match_ptr = search_strings( str1, str2, start_ptr );
if( match_ptr )
{
match_ctr ++;
if( debug_flag == 1 )
printf( "\n if - MATCH ctr %d: need to start copy/replace", match_ctr );
// RESULT STRING: copy from archive string start location to match location
copy_strings( start_ptr, match_ptr, str4, copy_ptr );
copy_ptr += match_ptr - start_ptr;
// RESULT STRING: copy entire replacement string, i.e. replace query string
copy_strings( ptr3, ptr3 + strlen( str3 ), str4, copy_ptr );
if( debug_flag == 1 )
printf( "\n MATCH: RESULT STR4: %s", str4 );
start_ptr = match_ptr + strlen(str2);
copy_ptr += strlen( str3 );
if( debug_flag == 1 )
printf( "\n continue searching from %c", *start_ptr );
}
else
{
if( debug_flag == 1 )
printf("\n else - match ctr = %d", match_ctr);
if( match_ctr != 0)
{
// RESULT STRING: copy from archive string last match location until end
if( debug_flag == 1 )
printf( "\n NO MATCH: copy from last match start ptr %c until end", *start_ptr );
copy_strings( start_ptr, str1 + strlen( str1 ), str4, copy_ptr );
if( debug_flag == 1 )
printf( "\n NO MATCH: RESULT STR4: %s", str4 );
break;
}
else
{
strcpy( str4, "The archive string does not contain the query string." );
break;
}
start_ptr ++;
}
}
display_results( str1, str2, str3, str4 );
return 0;
} // end main()
void display_help()
{
system( "tput clear" );
puts( " Welcome to the String Search and Replace Program." );
puts( " The Search and Replace Program searches the archive string for a query string." );
puts( " If the archive string contains the entire query string, it replaces the query" );
puts( " string with the replacement string and displays the final result string." );
puts( " Inputs: Archive String, Query String and Replacement String." );
puts( " Outputs: Result String" );
puts( " Example:" );
puts( " --------" );
puts( " Archive String: \"abcd\"" );
puts( " Query String: \"bc\"" );
puts( " Replacement String: \"1234\"" );
puts( " Result String: \"a1234d\"" );
puts( "" );
}
void get_strings( str1, str2, str3 )
char str1[32], str2[4], str3[8];
{
void get_string();
get_string( str1, "archive", 32 );
get_string( str2, "query", 4 );
get_string( str3, "replacement", 8 );
}
void get_string( str, name, max_len )
char str[32], name[16];
int max_len;
{
for(;;)
{
printf( " Enter the %s string (Maximum length = %d characters): ", name, max_len );
scanf( "%s", str );
fflush( stdin );
if( strlen( str ) <= max_len )
{
break;
}
}
}
char *search_strings( str1, str2, start_ptr )
char str1[32], str2[4];
char *start_ptr;
{
char *ptr1, *ptr2, *match_ptr1;
int ctr1, ctr2, match_ctr1;
ptr1 = ptr2 = match_ptr1 = NULL;
ctr1 = ctr2 = match_ctr1 = 0;
for( ptr1=start_ptr, ptr2=str2, ctr1=0; *ptr1!='\0'; ptr1++, ctr1++ )
{
if( debug_flag == 1 )
printf( "\n array index %d - comparing STR1: %c with STR2: %c", ctr1, *ptr1, *ptr2);
if( *ptr1 == *ptr2 )
{
// match_ptr1 = ptr1; // VC TEST
match_ctr1++;
ptr2++;
ctr2++;
if( *ptr2 == '\0' )
{
// calculate match start location
match_ptr1 = ptr1 - strlen ( str2 ) + 1; // match_ptr1 - strlen( str2 ) + 1;
if( debug_flag == 1 )
printf( "\n MATCH: STR1 contains STR2 at character %c", *match_ptr1 );
ptr2 = str2;
ctr2 = 0;
return match_ptr1;
}
}
else
{
if( debug_flag == 1 )
printf( "\n search_strings: NO MATCH ");
match_ptr1 = NULL;
ptr2 = str2;
ctr2 = 0;
}
}
// printf( "\n search_strings: match_ptr1 = %c", *match_ptr1 );
return match_ptr1;
}
void copy_strings( start_ptr, end_ptr, dest_str, dest_copy_ptr )
char dest_str[1024];
char *start_ptr, *end_ptr, *dest_copy_ptr;
{
char *ptr1, *ptr2;
for( ptr1 = start_ptr, ptr2 = dest_copy_ptr; ptr1 < end_ptr; ptr1 ++, ptr2 ++ )
{
*ptr2 = *ptr1;
if( debug_flag == 1 )
printf( "\n DEST STR: copied %c to %c", *ptr1, *ptr2 );
}
}
void display_results( str1, str2, str3, str4 )
char str1[32], str2[4], str3[8], str4[1024];
{
printf( "\n Archive String: %s", str1 );
printf( "\n Query String: %s", str2 );
printf( "\n Replacement String: %s", str3 );
printf( "\n Final Result String: %s", str4 );
printf( "\n" );
}