Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to Replace a word in a text by another given word
In this program, we have given three strings: text, oldword, and newword. Our task is to create a C program to replace a word in a text by another given word.
The program will search for all the occurrences of the oldword in the text and replace it with newword.
Syntax
void replaceWordInText(const char *text, const char *oldWord, const char *newWord);
Example: Input and Expected Output
Let's take an example to understand the problem −
Input:
text = "I am learning programming" oldword = "learning" newword = "practicing"
Output:
"I am practicing programming"
Algorithm
To solve this problem, we will first count the occurrences of the oldword in the string, then create a new string with sufficient memory to store the text with replaced words.
Example: Complete Program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void replaceWordInText(const char *text, const char *oldWord, const char *newWord) {
int i = 0, cnt = 0;
int len1 = strlen(newWord);
int len2 = strlen(oldWord);
// Count occurrences of oldWord
for (i = 0; text[i] != '\0'; i++) {
if (strstr(&text[i], oldWord) == &text[i]) {
cnt++;
i += len2 - 1;
}
}
// Allocate memory for new string
char *newString = (char *)malloc(i + cnt * (len1 - len2) + 1);
if (newString == NULL) {
printf("Memory allocation failed
");
return;
}
i = 0;
while (*text) {
if (strstr(text, oldWord) == text) {
strcpy(&newString[i], newWord);
i += len1;
text += len2;
}
else {
newString[i++] = *text++;
}
}
newString[i] = '\0';
printf("New String: %s
", newString);
free(newString);
}
int main() {
char str[] = "I am learning programming";
char oldWord[] = "learning";
char newWord[] = "practicing";
printf("Original string: %s
", str);
replaceWordInText(str, oldWord, newWord);
return 0;
}
Output
Original string: I am learning programming New String: I am practicing programming
How It Works
- First, the program counts how many times the
oldWordappears in the text usingstrstr(). - It calculates the required memory for the new string based on the difference in lengths between old and new words.
- Memory is allocated dynamically using
malloc()with proper NULL checking. - The program iterates through the original text, replacing occurrences of
oldWordwithnewWord. - Finally, it prints the modified string and frees the allocated memory.
Key Points
- The function uses
strstr()to find word occurrences in the text. - Dynamic memory allocation ensures the new string has sufficient space.
- Always check for NULL after
malloc()and usefree()to prevent memory leaks. - The algorithm handles multiple occurrences of the same word correctly.
Conclusion
This C program efficiently replaces all occurrences of a word in a text string using dynamic memory allocation and string manipulation functions. The approach ensures memory safety and handles variable-length replacements correctly.
