* String assignement
@ 2005-01-25 15:06 HIToC
2005-01-25 16:23 ` Ron Michael Khu
0 siblings, 1 reply; 2+ messages in thread
From: HIToC @ 2005-01-25 15:06 UTC (permalink / raw)
To: linux-c-programming
Hello all!
I have a string CMD_LN that may contain a substring within angle brackets.
I would like to assign another string IN_BRACKETS with the substring that is
contained within the angle brackets of CMD_LN if it is present.
I have written this code that should work:
// A_brack.cpp
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
string cmd_ln, in_brackets;
string::size_type bg, ed;
cmd_ln = "Hello <all>, hello world!\n";
bg = cmd_ln.find('<');
ed = cmd_ln.find('>', bg);
if(bg != ed != string::npos) in_brackets.assign(cmd_ln, bg, ed);
cout <<"Starting string:\t" <<cmd_ln <<'\n'
<<"Angle-brackets string:\t" <<in_brackets <<'\n'
<<"bg:\t" <<bg <<"\ned:\t" <<ed <<'\n'
<<cmd_ln[bg] <<' ' <<cmd_ln[ed] <<'\n';
return 0;
}
The output should be:
Starting string: Hello <all>, hello world!
Angle-brackets string: <all>
bg: 6
ed: 10
< >
But...it is not; this is the output of the program:
Starting string: Hello <all>, hello world!
Angle-brackets string: <all>, hel
bg: 6
ed: 10
< >
Thanks!
--
With regards,
HIToC
hitoc_mail@yahoo.it
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: String assignement
2005-01-25 15:06 String assignement HIToC
@ 2005-01-25 16:23 ` Ron Michael Khu
0 siblings, 0 replies; 2+ messages in thread
From: Ron Michael Khu @ 2005-01-25 16:23 UTC (permalink / raw)
To: HIToC; +Cc: linux-c-programming
Hi!
I dont know much about c++ but after playing around with ur code,
i discovered that u gave the assign() method a wrong value for the
second parameter...
why dont u try "ed-bg+1" ??
(remove the '-1' if u like)
i think the second parameter is count or len... not the end index
-Ron
HIToC wrote:
>Hello all!
>
>I have a string CMD_LN that may contain a substring within angle brackets.
>I would like to assign another string IN_BRACKETS with the substring that is
>contained within the angle brackets of CMD_LN if it is present.
>
>I have written this code that should work:
>
>
>// A_brack.cpp
>
>#include <iostream>
>#include <string>
>
>int main(int argc, char* argv[])
>{
> string cmd_ln, in_brackets;
> string::size_type bg, ed;
>
> cmd_ln = "Hello <all>, hello world!\n";
>
> bg = cmd_ln.find('<');
> ed = cmd_ln.find('>', bg);
> if(bg != ed != string::npos) in_brackets.assign(cmd_ln, bg, ed);
>
> cout <<"Starting string:\t" <<cmd_ln <<'\n'
> <<"Angle-brackets string:\t" <<in_brackets <<'\n'
> <<"bg:\t" <<bg <<"\ned:\t" <<ed <<'\n'
> <<cmd_ln[bg] <<' ' <<cmd_ln[ed] <<'\n';
>
> return 0;
>}
>
>
>The output should be:
>Starting string: Hello <all>, hello world!
>
>Angle-brackets string: <all>
>bg: 6
>ed: 10
>< >
>
>
>But...it is not; this is the output of the program:
>
>Starting string: Hello <all>, hello world!
>
>Angle-brackets string: <all>, hel
>bg: 6
>ed: 10
>< >
>
>
>
>
>
>Thanks!
>
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-01-25 16:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-25 15:06 String assignement HIToC
2005-01-25 16:23 ` Ron Michael Khu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).