-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFull_Program.y
More file actions
397 lines (344 loc) · 11 KB
/
Full_Program.y
File metadata and controls
397 lines (344 loc) · 11 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_LENGTH 1000
// Global variables
char sentence[MAX_LENGTH] = "";
char concatenated[MAX_LENGTH] = "";
char search_word[100] = "";
char new_word[100] = "";
// Function prototypes
void reverseString(char* str);
void countVowelsConsonants(char* str);
void addWordToString();
void deleteWord();
void replaceWord();
void findPalindrome();
void countWordsLinesSpacesChars();
void changeCase();
void concatenateStrings();
void PrintWithLength(const char* input);
void PrintLongestWord(const char* input);
void resetBuffer();
void yyerror(const char* s) {
fprintf(stderr, "Error: %s\n", s);
}
int yylex();
%}
%union {
char* str_val;
}
%token REVERSE VOWELS_CONSONANTS VIEW ADD_WORD DELETE_WORD REPLACE_WORD PALINDROME COUNT CHANGE_CASE PRINT_STRINGS LONGEST_STRING
%token <str_val> WORD STRING
%token PLUS SPACE NEWLINE CHAR
%token CONCATENATE
%%
program:
| program statement
;
statement:
REVERSE {
reverseString(sentence);
printf("Updated string: %s\n", sentence);
}
| VOWELS_CONSONANTS {
countVowelsConsonants(sentence);
}
| VIEW {
printf("\nStored string: %s\n", sentence);
}
| ADD_WORD {
addWordToString();
printf("Updated string: %s\n", sentence);
}
| DELETE_WORD {
deleteWord();
printf("Updated string: %s\n", sentence);
}
| REPLACE_WORD {
replaceWord();
printf("Updated string: %s\n", sentence);
}
| PALINDROME {
findPalindrome();
}
| COUNT {
countWordsLinesSpacesChars();
}
| CHANGE_CASE {
changeCase();
}
| CONCATENATE {
concatenateStrings();
}
| PRINT_STRINGS {
PrintWithLength(sentence);
}
| LONGEST_STRING{
PrintLongestWord(sentence);
}
;
;
%%
int main() {
printf("Please enter a string: ");
fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0';
int choice;
do {
printf("\n----------------------------------------------\n");
printf(" String Processing \n");
printf("----------------------------------------------\n");
printf("0. Exit\n");
printf("1. View stored string\n");
printf("2. Add new word to string\n");
printf("3. Delete word from string\n");
printf("4. Replace word in string\n");
printf("5. Reverse the string\n");
printf("6. Count number of vowels and consonants\n");
printf("7. Concatenate strings\n");
printf("8. String case conversion\n");
printf("9. Find palindromes\n");
printf("10. Count words, lines, spaces, and characters\n");
printf("11. Print length of each string\n");
printf("12. Print longest length\n");
printf("Enter option: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1: yy_scan_string("view"); yyparse(); break;
case 2: yy_scan_string("add"); yyparse(); break;
case 3: yy_scan_string("delete"); yyparse(); break;
case 4: yy_scan_string("replace"); yyparse(); break;
case 5: yy_scan_string("reverse"); yyparse(); break;
case 6: yy_scan_string("vowels_consonants"); yyparse(); break;
case 7: yy_scan_string("concat"); yyparse(); break;
case 8: yy_scan_string("case"); yyparse(); break;
case 9: yy_scan_string("palindrome"); yyparse(); break;
case 10: yy_scan_string("count"); yyparse(); break;
case 11: yy_scan_string("print_strings"); yyparse(); break;
case 12: yy_scan_string("longest"); yyparse(); break;
case 0: printf("Exiting program. Goodbye!\n"); break;
default: printf("Invalid option. Try again.\n"); break;
}
} while (choice != 0);
return 0;
}
// Function definitions
void reverseString(char* str) {
int len = strlen(str);
char temp[MAX_LENGTH] = "";
int i;
for (i = len - 1; i >= 0; i--) {
strncat(temp, &str[i], 1);
}
strcpy(str, temp);
}
void countVowelsConsonants(char* str) {
int vowels = 0, consonants = 0, others = 0;
int i;
for (i = 0; str[i]; i++) {
char c = tolower(str[i]);
if (strchr("aeiou", c)) {
vowels++;
} else if (isalpha(c)) {
consonants++;
} else if (!isspace(c) && c != '\0') {
others++;
}
}
printf("\nNumber of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
printf("Number of other characters: %d\n", others);
}
void findPalindrome() {
char temp[MAX_LENGTH];
strcpy(temp, sentence);
char* word = strtok(temp, " \n");
int found = 0;
while (word != NULL) {
int len = strlen(word);
int is_palindrome = 1;
int i;
for (i = 0; i < len / 2; i++) {
if (tolower(word[i]) != tolower(word[len - i - 1])) {
is_palindrome = 0;
break;
}
}
if (is_palindrome) {
printf("Palindrome found: %s\n", word);
found = 1;
}
word = strtok(NULL, " \n");
}
if (!found) {
printf("No palindromes found.\n");
}
}
void countWordsLinesSpacesChars() {
int words = 0, lines = 1, spaces = 0, chars = 0;
int in_word = 0;
int i;
for (i = 0; sentence[i]; i++) {
chars++;
if (sentence[i] == '\n') {
lines++;
}
if (isspace(sentence[i])) {
spaces++;
in_word = 0;
} else if (!in_word) {
words++;
in_word = 1;
}
}
printf("\nWords: %d\n", words);
printf("Lines: %d\n", lines);
printf("Spaces: %d\n", spaces);
printf("Characters: %d\n", chars);
}
void changeCase() {
int mode;
printf("Choose mode (1: all lowercase, 2: all uppercase, 3: swap case): ");
scanf("%d", &mode);
getchar();
int i;
for (i = 0; sentence[i]; i++) {
if (mode == 1) {
sentence[i] = tolower(sentence[i]);
} else if (mode == 2) {
sentence[i] = toupper(sentence[i]);
} else {
if (isupper(sentence[i])) {
sentence[i] = tolower(sentence[i]);
} else if (islower(sentence[i])) {
sentence[i] = toupper(sentence[i]);
}
}
}
printf("Updated string: %s\n", sentence);
}
void concatenateStrings() {
char input[MAX_LENGTH];
concatenated[0] = '\0'; // Reset concatenated buffer
printf("Enter the string you wish to concatenate: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove trailing newline
// Remove spaces and concatenate words
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace(input[i])) {
concatenated[j++] = input[i];
}
}
concatenated[j] = '\0'; // Null-terminate the string
printf("Concatenated string: %s\n", concatenated);
}
void resetBuffer() {
concatenated[0] = '\0';
}
void addWordToString() {
printf("Enter the existing word to modify: ");
fgets(search_word, sizeof(search_word), stdin);
search_word[strcspn(search_word, "\n")] = '\0';
printf("Enter the new word to add: ");
fgets(new_word, sizeof(new_word), stdin);
new_word[strcspn(new_word, "\n")] = '\0';
printf("Where to place the new word relative to '%s'?\n", search_word);
printf("1. Front\n2. Back\n");
int position;
scanf("%d", &position);
getchar();
char* pos = strstr(sentence, search_word);
if (!pos) {
printf("Word not found in string.\n");
return;
}
char temp[MAX_LENGTH];
if (position == 1) {
snprintf(temp, sizeof(temp), "%.*s%s %s%s", (int)(pos - sentence), sentence, new_word, search_word, pos + strlen(search_word));
} else {
snprintf(temp, sizeof(temp), "%.*s%s %s%s", (int)(pos - sentence), sentence, search_word, new_word, pos + strlen(search_word));
}
strcpy(sentence, temp);
}
void deleteWord() {
printf("Enter the word to delete: ");
fgets(search_word, sizeof(search_word), stdin);
search_word[strcspn(search_word, "\n")] = '\0';
char* pos = strstr(sentence, search_word);
if (!pos) {
printf("Word not found in string.\n");
return;
}
char temp[MAX_LENGTH];
snprintf(temp, sizeof(temp), "%.*s%s", (int)(pos - sentence), sentence, pos + strlen(search_word));
// Remove extra space
char* end_space = strstr(temp, " ");
if (end_space) {
memmove(end_space, end_space + 1, strlen(end_space));
}
strcpy(sentence, temp);
}
void replaceWord() {
printf("Enter the word to replace: ");
fgets(search_word, sizeof(search_word), stdin);
search_word[strcspn(search_word, "\n")] = '\0';
printf("Enter the new word: ");
fgets(new_word, sizeof(new_word), stdin);
new_word[strcspn(new_word, "\n")] = '\0';
char* pos = strstr(sentence, search_word);
if (!pos) {
printf("Word not found in string.\n");
return;
}
char temp[MAX_LENGTH];
snprintf(temp, sizeof(temp), "%.*s%s%s", (int)(pos - sentence), sentence, new_word, pos + strlen(search_word));
strcpy(sentence, temp);
}
void PrintWithLength(const char* input) {
int MAX_WORDS = 100;
int MAX_WORD_LENGTH = 100;
char words[MAX_WORDS][MAX_WORD_LENGTH];
int count = 0;
// Temporary copy of input to tokenize
char temp[strlen(input) + 1];
strcpy(temp, input);
// Tokenize the string using space as a delimiter
char* token = strtok(temp, " ");
while (token != NULL && count < MAX_WORDS) {
strncpy(words[count], token, MAX_WORD_LENGTH - 1);
words[count][MAX_WORD_LENGTH - 1] = '\0';
count++;
token = strtok(NULL, " ");
}
// Print each word and its length
printf("Split words and their lengths:\n");
int i=0;
for (i = 0; i < count; i++) {
printf("Word: '%s', Length: %lu\n", words[i], strlen(words[i]));
}
}
void PrintLongestWord(const char* input) {
char temp[strlen(input) + 1];
strcpy(temp, input);
char* token = strtok(temp, " ");
char* longest_word = NULL;
int max_length = 0;
while (token != NULL) {
int length = strlen(token);
if (length > max_length) {
max_length = length;
longest_word = token;
}
token = strtok(NULL, " ");
}
if (longest_word) {
printf("Longest word: '%s', Length: %d\n", longest_word, max_length);
} else {
printf("No words found.\n");
}
}