-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path42.c
46 lines (36 loc) · 1.2 KB
/
42.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
#include <stdio.h>
#define MAX_LENGTH 1000 // Maximum length of the input string
// Function to replace occurrences of "the" with "a"
void replaceTheWithA(char *str) {
char result[MAX_LENGTH]; // To store the modified string
int i = 0, j = 0;
while (str[i] != '\0') {
// Check if the current position starts with "the"
if (str[i] == 't' && str[i + 1] == 'h' && str[i + 2] == 'e') {
result[j] = 'a'; // Replace "the" with "a"
j++; // Move to the next position in the result string
i += 3; // Skip past "the"
} else {
// Otherwise, copy the current character to the result string
result[j] = str[i];
j++;
i++;
}
}
result[j] = '\0'; // Null-terminate the new string
// Copy the result back to the original string
for (int k = 0; k <= j; k++) {
str[k] = result[k];
}
}
int main() {
char str[MAX_LENGTH];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Replace occurrences of "the" with "a"
replaceTheWithA(str);
// Output the modified string
printf("Modified string: %s", str);
return 0;
}