1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string addSpaces( string text, vector<string> allStrings )
{
if ( text == "" ) return text; // Trivial case
for ( string s : allStrings ) // Put a space before and after any specified string
{
int pos = 0;
while( true )
{
pos = text.find( s, pos );
if ( pos == string::npos ) break;
if ( pos != 0 ) // Don't want a space at the start
{
text.insert( pos, " " ); pos++;
}
pos += s.size();
if ( pos >= text.size() ) break; // Don't want a space at the end
text.insert( pos, " " ); pos++; // Comment out this line if you don't want double-spacing
}
}
return text;
}
int main()
{
vector<string> fileNames{ "File1", "File2", "File12" };
vector<string> expNames{ "A", "B", "C", "R1", "R2" };
vector<string> opNames{ "+", "-", "*", "/", "=" };
// allStrings will contain anything that you need a space preceding
vector<string> allStrings = fileNames;
for ( string s : expNames ) allStrings.push_back( s );
for ( string s : opNames ) allStrings.push_back( s );
string test;
test = "R2R1R2+*R1+"; cout << "\nTest: " << test << "\nResult: " << addSpaces( test, allStrings ) << endl;
test = "ABCR1R2+ABC"; cout << "\nTest: " << test << "\nResult: " << addSpaces( test, allStrings ) << endl;
test = "A+BunknownC"; cout << "\nTest: " << test << "\nResult: " << addSpaces( test, allStrings ) << endl;
test = "File1+R1ACC"; cout << "\nTest: " << test << "\nResult: " << addSpaces( test, allStrings ) << endl;
}
|