1

My question is, how can I split a string in C++? For example, I have `

string str = "[ (a*b) + {(c-d)/f} ]"
  1. It need to get the whole expression individually like [,(,a,*,b,......
  2. And I want to get the brackets only like [,(,),{,(,),},] on their proper position

How can I do these with some easy ways

Mat
  • 202,337
  • 40
  • 393
  • 406
Husnain Iqbal
  • 466
  • 6
  • 16
  • possible duplicate of [Splitting a string in C++](http://stackoverflow.com/questions/236129/splitting-a-string-in-c) – paulsm4 Sep 15 '13 at 08:33
  • I doubt there is a way where you can do "both of these at once" (other than writing your own parser that does one thing for each toke, and something else for the brackets). – Mats Petersson Sep 15 '13 at 08:35
  • 1
    There are no easy ways to do this in C++. There are functions for finding strings or characters in strings, and functions for extracting substrings from strings. But you have to write the code that puts these together to do precisely what you want. – john Sep 15 '13 at 08:36
  • Why exactly do you ask? Is this a homework? Do you want a calculator? – Basile Starynkevitch Sep 15 '13 at 08:54
  • No, perhaps you may have read the applications of stack; it contains 1.Verifying the brackets in an expression and 2. calculating the infix/postfix expressions. I want to implement that – Husnain Iqbal Sep 15 '13 at 09:09
  • Indeed, parsers use stacks. But why do you want a parser? What for? What is your overall goal? Give some larger picture please! Is this just for learning parsing techniques? Then read a good book (e.g. the [Dragon Book](http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools) ...) about compiler techniques.... – Basile Starynkevitch Sep 15 '13 at 09:12
  • From your examples it seems you want to things: 1. print each character of the string, with delimiting commas in between 2. same as 1, put filter on certain symbols. Is that really what you want? – Zane Sep 15 '13 at 14:25
  • yup, you are correct 1. I want all the characters individually and 2. all brackets with the order in the expression – Husnain Iqbal Sep 15 '13 at 16:58

2 Answers2

3

This is called lexical analysis (getting tokens from some sequence or stream of characters) and should be followed by parsing. Read e.g. the first half of the Dragon Book.

Maybe LL parsing is enough for you....

There are many tools for that, see this question (I would suggest ANTLR). You probably should build some abstract syntax tree at some point.

But it might not worth the effort. Did you consider embedding some scripting language in your application, e.g. lua (see this and this...), or gnu guile, python, etc...

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Here is a way I got to do this,

string expression = "[ (a*b) + {(c-d)/f} ]" ;
string token ;

// appending an extra character that i'm sure will never occur in my expression 
// and will be used for splitting here

expression.append("~") ; 
istringstream iss(expression);
getline(iss, token, '~');

for(int i = 0 ; i < token.length() ; i++ ) {
    if(token[i] != ' ' ) {
        cout<<token[i] << ",";
    }
}

Output will be: [,(,a,*,b,),+,{,(,c,-,d,),/,f,},],

Husnain Iqbal
  • 466
  • 6
  • 16