Click to See Complete Forum and Search --> : Print the characters excluding the specified substring


sree1503vani
September 28th, 2005, 04:17 PM
Hi,
I have a string str ="this is my name"
start = 's'
end = 'n'
my out put should exclude the chars between s and n and print the rest of the string

str = "thi ame" as my output string.

Can anyone please help me with c++ code to this problem...

Thanks

MrViggy
September 28th, 2005, 04:38 PM
Well, you could loop over each character in the string; when you find an 's', you skip each character, until you find the 'n'.

Write up some code, and if you have any questions about the code, just ask.

Viggy

golanshahar
September 28th, 2005, 04:45 PM
you can do something like that:

#include <string>

std::string lpsz="this is my name";
std::string start ="s";
std::string end ="n";

int pos1 = lpsz.find_first_of(start);
int pos2 = lpsz.find_last_of(end);

std::string result;
if ( pos1 >= 0 )
{
result = lpsz.substr(0,pos1);
result += " ";
}
if ( pos2 >= 0 && pos2 < lpsz.length())
result += lpsz.substr(pos2+1,lpsz.length());

// result = "thi ame"



NOTE: this code want not completely debuged!!!

Cheers