|
-
September 28th, 2005, 04:17 PM
#1
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
-
September 28th, 2005, 04:38 PM
#2
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
-
September 28th, 2005, 04:45 PM
#3
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|