I am working on a program for school where a user enters a search term (ie. abcde) and also a sentence to find the string in. I am almost done it but am having one problem.
Example:
Please enter a search string: abcde
Please enter a sentence to search: this is a test abcde please help
Output: this is a test ***** please help
So I know how to get the length of the string and find the string, but I need some help in replacing the string.
I have a function reverseString(string InputText, string searchStr, string replaceStr) which works if I declare
string replaceStr = "*";
But how do I use the size of the searchStr to make replaceStr the same amount of *'s?
Find the location of the string by using string.find(). Then use string.substring(0, location) to get the first half, a simple for-loop adding the * and the a string.substring(location, length) to add the rest of the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
usingnamespace std;
int main() {
string findMe = "some";
string findIn = "this is some really strange sentence";
string answer = "";
// Code omitted so I am not doing your homework for you :P
cout << answer << endl;
return 0;
}