Hi all, I've written most of the code for this assignment, however I'm stuck on one part. Here's the assignment:
Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, string3. It should search string1 for all occurrences of string2. When it finds an occurrence of string2, it should replace it with string3. For example, suppose the three arguments have the following values:
string1: "the dog jumped over the fence"
string2: "the"
string3: "that"
With these three arguments, the function would return a string object with the value "that dog jumped over that fence". Demonstrate the function in a complete program.
My problem is, when I have the user input the string3, I'm not sure how I should go about replacing anything found with string2 in string1.
Just rewrite the string character by character. If there's the to-be-replaced string at the current position, append the replacement string, otherwise append the current character.
If you want to use strstr, you have to append the entire substring between any matches you found.
You access the character at index i with str[i]. If you want to find a substring in a C string, you can use strstr. When you use std::string (which you should do), use the find member function. See: http://www.cplusplus.com/reference/string/string/
Write a function named replaceSubstring. The function should accept three C-string or string object arguments.
Do you think you would be allowed to write the function to accept an object of string objects? I ask because the first thing I thought after reading your question was vectors.
You could read in a line of text, separate each individual word in that text, storing them in a vector of string. Then you can search that vector one element at a time, checking each value against the search word, and finally switching any found words with the choosen replacement.
It seems like my code now can find each letter, it's the replacing part I'm unsure about. The vector idea seems sound, however I wouldn't have the slightest clue on writing all of that.
so the string to search for can be multiple words? hmm, is it possible to count the number of words in the search string (X), then iterate over all sets of X consecutive words in the original text until (or if) a matching phrase is found?
EDIT: nvm, I never knew that strstr() was a member function. I had just assumed it was your user defined function to replace the strings.
That's because the '6' in strncpy (strPtr,string3,6);
should be a '4' (or, more generally, the length of the replacement string) strncpy (strPtr, string3, strlen(string3);
But that seems to introduce another problem: the replacement string is overwriting characters in the original string.
I ran the code with
string1 = "the dogs"
string2 = "the"
string3 = "those"
Yes, more or less. Since the replacing is not restricted to entire words, it would be: string4=character + character + [...] + replacedString + character + [...];
It's a primitive solution, but since it's so simple, there's no room for messing up.