Replacing a string with variable sizes

Hi,

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?

Any ideas would be helpful!

Many thanks
-Bec



I would proabably use the string functions .erase() and .insert(), using the string they entered's .size() to know how many * you need to insert.
Ahh yea. I was going to answer this earlier.

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>

using namespace 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;
}


this is **** really strange sentence
Topic archived. No new replies allowed.