C program to Replace a word in a text by another given word
Given three strings ‘str’, ‘oldW’ and ‘newW’. The task is find all occurrences of the word ‘oldW’ and replace then with word ‘newW’.
Examples:
Input : str[] = "xxforxx xx for xx",
oldW[] = "xx",
newW[] = "geeks"
Output : geeksforgeeks geeks for geeks
The idea is to traverse the original string and count the number of times old word occurs in the string. Now make a new string of sufficient size so that new word can be replaced. Now copy original string to new string with replacement of word.
// C program to search and replace // all occurrences of a word with // other word. #include <stdio.h> #include <string.h> #include <stdlib.h> // Function to replace a string with another // string char *replaceWord(const char *s, const char *oldW, const char *newW) { char *result; int i, cnt = 0; int newWlen = strlen(newW); int oldWlen = strlen(oldW); // Counting the number of times old word // occur in the string for (i = 0; s[i] != '\0'; i++) { if (strstr(&s;[i], oldW) == &s;[i]) { cnt++; // Jumping to index after the old word. i += oldWlen - 1; } } // Making new string of enough length result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1); i = 0; while (*s) { // compare the substring with the result if (strstr(s, oldW) == s) { strcpy(&result;[i], newW); i += newWlen; s += oldWlen; } else result[i++] = *s++; } result[i] = '\0'; return result; } // Driver Program int main() { char str[] = "xxforxx"; char c[] = "xx"; char d[] = "Geeks"; char *result = NULL; // oldW string printf("Old string: %sn", str); result = replaceWord(str, c, d); printf("New String: %sn", result); free(result); return 0; } |
Output:
Old string: xxforxx New String: GeeksforGeeks
Method 2(Inplace update of string, uses extra space only for the new characters to be inserted)
#include<bits/stdc++.h> using namespace std; int main() { //code int t; cin>>t; cin.ignore(); while(t--) { string s; getline(cin, s); string x, y; getline(cin, x); getline(cin, y); reverse(s.begin(), s.end()); reverse(x.begin(), x.end()); reverse(y.begin(), y.end()); int ls = s.length(), lx = x.length(), ly = y.length(); int d = ly - lx; int ct = 0; int i = 0, j=0; while(i<ls) { string temp = ""; for(int k=0;k<lx;k++) { temp += s[i+k]; } if(temp == x) { ct++; i = i+lx; } else { i = i+1; } } for(int i=0;i<ct*d;i++) s += ' '; i = ls-1; j = ls+ct*d - 1; while(i>=0 && j>=0) { string temp = ""; for(int k=0;k<lx;k++) { if(i<(lx-1-k)) break; temp += s[i-(lx-1-k)]; } if(temp == x) { int k=ly-1; while(k>=0) s[j--] = y[k--]; i = i - lx; } else { s[j--] = s[i--]; } } reverse(s.begin(), s.end()); cout<<s<<endl; } return 0; } |
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- A Program to check if strings are rotations of each other or not
- Write a program to print all permutations of a given string
- Write a program to reverse an array or string
- Print list items containing all characters of a given word
- Program to validate an IP address
- Recursively print all sentences that can be formed from list of word lists
- Longest Common Prefix using Word by Word Matching
- In-place replace multiple occurrences of a pattern
- Word Break Problem using Backtracking
- Program to toggle all characters in a string
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Find largest word in dictionary by deleting some characters of given string
- Replace all occurrences of string AB with C without using extra space
- Program to find second most frequent character
- C Program to Sort an array of names or strings
Improved By : Didi17



