Hi,

I have a string and I'm trying to replaces unwanted characters with a space character.

So if have the string "A string of TEXT!" and I replace the chars "sT" I end up with "A tring of EX !".

I came up with a method, but it was inefficient, so I've tried to give the binary_search() method a go, but am a little stuck.

My problem is that the all the elements in the vector need to be sorted for a binary_search() to work, but if I sort the vector which holds the sentence, then all the letters will become jumbled up, and so will make no sense to the reader.

Here is my program:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void main()
{
	/***********************************************************
	put each char from a string into a vector
	************************************************************/
	
	//the string to use
	string str = "A string of T3XT!";

	//the string will go into this vector
	vector<char>strVec1;

	//populate that vector with the string
	for(unsigned int i = 0; i < str.length(); ++i)
	{
		strVec1.push_back(str[i]);
	}

	/**************************************************************
	put each char from a second string into a second vector
	***************************************************************/

	//the second string to use
	string charsToReplace = "sT";
	
	//the second string will go into this vector
	vector<char>strVec2;

	//populate the second vector with the second string
	for(unsigned int ii = 0; ii < charsToReplace.size(); ++ii)
	{
		strVec2.push_back(charsToReplace[ii]);
	}


	/**************************************************************
		sort both vectors so they can be binary_search'd
	**************************************************************/

	//Problem, if I sort() the first vector, then all the chars(ie: the
	//letters in the sentence will be out of order, and so it will 
	//make no sense

	/**************************************************************
	replace any unwanted chars with a space
	***************************************************************/
	//if any of the chars in the first vector match any of the chars 
	//in the second vector, replace the char in the 
	//first vector with a space.

	/**************************************************
		old inefficient method:
	**************************************************/

	//for(unsigned int a = 0;a<strVec1.size();++a)
	//{       
	//        for(unsigned int j = 0; j < strVec2.size(); ++j)
	//        {       
	//                if(strVec1[a] == strVec2[j])//if the two chars match
	//                {
	//                        strVec1[a] = ' ';//replace the char from the second with a space
	//                        cout << "0";
	//                }
	//        }
	//}


	/**************************************************
		new binary search method:
	**************************************************/
	for(unsigned int a = 0;a<strVec1.size();++a)
	{	
		if(binary_search(strVec1.begin(), strVec1.end(), strVec2[a]))
		{
			strVec1[a] = ' ';
		}
	}

	/************************************************************
		put all the remaining chars back into the original string
	*************************************************************/
	
	//empty the old string variable
	str = "";

	//put the remaining chars back into the string
	for(unsigned int b = 0; b < strVec1.size(); ++b)
	{
		str += strVec1[b];
		cout << str << endl;
	}
	
	cout << "all unwanted characters removed from the string." << endl;
	
	cin.get();
}
I'm looking to test for the 255 ASCII characters.

How do you think I should tackle this problem?

Your advice is really appreciated.

Many thanks!