473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C++: Help output nested strings

Could you please explain me how can I output nested strings?

Here is an example:

"adsd{rf[dfF]km}xcv"

The output should start from the inner parentheses, such as:

dfF
rfkm
adsdxcv
Thanks!

Feb 20 '06 #1
9 2155
Diane wrote:
Could you please explain me how can I output nested strings?

Here is an example:

"adsd{rf[dfF]km}xcv"

The output should start from the inner parentheses, such as:

dfF
rfkm
adsdxcv


This isn't a C++ problem. You need to build those strings.
It's a programming problem. What you have is a stack of
strings represented in a certain grammatical form. What you
need to do is to convert one representation into the other
and then output the other (the stack of strings). How to
convert one into the other is not a _language_ problem. You
are supposed to come up with an algorithm.

Come back once you have the algorithm and we'll help you put
it into C++ terms. Of course, if you have any C++ experience
at all, try writing your program and then we can help you get
it to work.

Also, as I see it, the string building function should probably
be recursive. It should start building its string either at the
beginning or right after getting an opening bracket/brace/paren
and end with the matching closing bracket/brace/paren or at the
end of the input. Once the function built the string it can
simply output it.

V
--
Please remove capital As from my address when replying by mail
Feb 20 '06 #2
Thanks for your reply Victor!

Here is what I tried to do, but it doesn't seem to work properly.
Any suggestions?

int main()
{
Stack s;
char symbol;
int counter = -1;
char match;

// read all characters from keyboard and push them in the stack
while((symbol=cin.get()) != '\n')
{
s.push(symbol);
counter++;
}
//scan the stack to output the nested strings
for(int i=0; i<counter; i++)
{

if(symbol == '{' || symbol == '(' || symbol == '[')
{
cout << "\n";
match = symbol;
}

if(symbol == '}' || symbol == ')' || symbol == ']')
{
if( (symbol == '}' && match == '{') || (symbol == ')' &&
match == '(')
|| (symbol == ']' && match == '[') )
{
s.pop();
}
}

}

return 0;

}

Feb 21 '06 #3
Diane wrote:
Here is what I tried to do, but it doesn't seem to work properly.
Any suggestions?
You need to fix it.

[...]
if(symbol == '{' || symbol == '(' || symbol == '[')
{
cout << "\n";
Wouldn't it make more sense to do all output at the _closing_
character?
match = symbol;
}

if(symbol == '}' || symbol == ')' || symbol == ']')
{
if( (symbol == '}' && match == '{') || (symbol == ')' &&
match == '(')
|| (symbol == ']' && match == '[') )
{
s.pop();
... and you're losing the string here instead of outputting it...
}
}

V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #4
In article <11*********************@f14g2000cwb.googlegroups. com>,
"Diane" <fr*********@hotmail.com> wrote:
Could you please explain me how can I output nested strings?

Here is an example:

"adsd{rf[dfF]km}xcv"

The output should start from the inner parentheses, such as:

dfF
rfkm
adsdxcv


Paste the code below in your program (replace what you have) and play
with the code in the "parse_string" function until it works. Instead of
sending output to "cout" send it to "os".

void parse_string( const string& s, ostream& os ) {
// change the code in this method
os << s;
}

int main() {
string test( "hello" );
stringstream ss;
parse_string( test, ss );
assert( ss.str() == test );

test = "Good[hello]by";
ss.str( "" );
parse_string( test, ss );
assert( ss.str() == "hello\nGoodby" );

test = "Good(hello)by";
ss.str( "" );
parse_string( test, ss );
assert( ss.str() == "hello\nGoodby" );

cout << "working\n";
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #5
Daniel T. wrote:
In article <11*********************@f14g2000cwb.googlegroups. com>,
"Diane" <fr*********@hotmail.com> wrote:

Could you please explain me how can I output nested strings?

Here is an example:

"adsd{rf[dfF]km}xcv"

The output should start from the inner parentheses, such as:

dfF
rfkm
adsdxcv

Paste the code below in your program (replace what you have) and play
with the code in the "parse_string" function until it works. Instead of
sending output to "cout" send it to "os".

void parse_string( const string& s, ostream& os ) {
// change the code in this method


I recommend the boost::spirit framework to create a simple parser. Question
for the OP: do you want to output strings such as
"abc{xyz}ijk{ABC[lmn]dkd[123]}uiop{[sdf]}"? First you must define a set of
rules the matched strings must adhere to (number of nestings allowed, etc.)
before you can do anything.
os << s;
}


<snip>

--
To reply, take of all ZIGs !!
Feb 21 '06 #6
What does the parse_string method suppose to do?

I think that the stringstream works only with whitespaces. What if I
have other delimiters?

Feb 21 '06 #7
In article <11**********************@g44g2000cwa.googlegroups .com>,
"Diane" <fr*********@hotmail.com> wrote:
What does the parse_string method suppose to do?
In the long term, it's supposed to output nested strings like your
example. For now, it's supposed to make the tests in main pass.

Notice that the last line in 'main' is [cout << "working\n";] Your job
is to put code in "parse_string" until the program outputs "working"
Once you have done that, you will be one step closer to having your
assignment done.
I think that the stringstream works only with whitespaces. What if I
have other delimiters?


For your purposes, stringstream works exactly like cout, except it
outputs to a string rather than the screen.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #8
Thanks!

Feb 21 '06 #9
In article <11**********************@g47g2000cwa.googlegroups .com>,
"Diane" <fr*********@hotmail.com> wrote:
Thanks!


Check your "fr*********@hotmail.com" account email...

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Marko Faldix | last post by:
Hello, with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this piece of code: import codecs f = codecs.open("klotentest.txt", "w", "latin-1") print >>f, unicode("My umlauts...
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
9
by: a | last post by:
I need to write a regular expression to match a quoted string in which the double quote character itself is represented by 2 double quotes. For example: "beginning ""nested quoted string"" end"...
1
by: Henrik Bechmann | last post by:
All, I'm trying to spoof Google's vertical tabs in a vertical menu structured with nested UL/LI elements. To do this, I need to find out where the anchor in the LI is, and then create an...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
0
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.