This is a nice piece of program I wrote a few months ago. One of my students requested me to write this. So, here it is! Hope this might be useful for you…
#1: What the program does:
This program finds all the occurrences of a sub-string within a given string and replaces that with our provided string. Basically there are many similar programs but, most of them lack the ability to find multiple occurrences. So I wrote this keeping in mind that the search string may appear many times in the original string. Here is the algo:
- Get the original, search and replace string from the user.
- Write a user defined function to replace the first occurrence of the search string with the replace string.
- Recursively call the function until there is no occurrence of the search string.
Yes, there are many flaws in this program, like if the search string is a part of the replace string, the function will fail. I will try to remove these bugs when I get enough time. But this program will do the job for you in your college semesters 😉
#2: Source Code:
/** ****************************************************| * String replace Program | ****************************************************| * Takes three string input from the user * Replaces all the occurances of the second string * with the third string from the first string * @author Swashata */ /** Include Libraries */ #include <stdio.h> #include <stdlib.h> #include <string.h> /** Define the max char length */ #define MAX_L 4096 /** Prototypes */ void replace (char *, char *, char *); int main(void) { char o_string[MAX_L], s_string[MAX_L], r_string[MAX_L]; //String storing variables printf("Please enter the original string (max length %d characters): ", MAX_L); fflush(stdin); gets(o_string); printf("\nPlease enter the string to search (max length %d characters): ", MAX_L); fflush(stdin); gets(s_string); printf("\nPlease enter the replace string (max length %d characters): ", MAX_L); fflush(stdin); gets(r_string); printf("\n\nThe Original string\n*************************************\n"); puts(o_string); replace(o_string, s_string, r_string); printf("\n\nThe replaced string\n*************************************\n"); puts(o_string); return 0; } /** * The replace function * * Searches all of the occurrences using recursion * and replaces with the given string * @param char * o_string The original string * @param char * s_string The string to search for * @param char * r_string The replace string * @return void The o_string passed is modified */ void replace(char * o_string, char * s_string, char * r_string) { //a buffer variable to do all replace things char buffer[MAX_L]; //to store the pointer returned from strstr char * ch; //first exit condition if(!(ch = strstr(o_string, s_string))) return; //copy all the content to buffer before the first occurrence of the search string strncpy(buffer, o_string, ch-o_string); //prepare the buffer for appending by adding a null to the end of it buffer[ch-o_string] = 0; //append using sprintf function sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string)); //empty o_string for copying o_string[0] = 0; strcpy(o_string, buffer); //pass recursively to replace other occurrences return replace(o_string, s_string, r_string); }
#3: Output:
So that was it. If you have any better programming approach, then do share with us!
This info is useful for me and very important for me. Thank you for providing such a good info.
Thanx a lot for the program. It does have one big weakness, however. If the searched string is a sub-string of the replacement string, the program freezes. This is because the replace function will always keep finding the searched string however much it replaces the same with the replacement string, leading to an ever increasing number of recursive calls and finally memory shortness! Please try to rectify that.
Which software are you using? in the first image of this post.
It is Dev C++. Google it, it is free
pls send me the output of following program:
a)Write a program to calculate the sum of the following series
|x| -x2/3! +|x|3/6!-x4/10! +………….. n terms
Don’t use any library function of math.h, Define all the internal functions recursively.
hi ,
please find below soln,
void replace(char * o_string, char * s_string, char * r_string) {
//a buffer variable to do all replace things
char buffer[MAX_L]={0};
//to store the pointer returned from strstr
char * ch=NULL;
//first exit condition
if(!(ch = strstr(o_string, s_string)))
return;
//copy all the content to buffer before the first occurrence of the search string
strncpy(buffer, o_string, ch-o_string);
strcat(buffer,r_string);
strcat(buffer,ch+strlen(s_string));
memset(o_string,0,strlen(o_string));
strncpy(o_string,buffer,strlen(buffer));
replace(ch+strlen(s_string)-1,s_string,r_string);
}