Print the characters excluding the specified substring
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
Re: Print the characters excluding the specified substring
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
Re: Print the characters excluding the specified substring
you can do something like that:
Code:
#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