Thread: breaking string into substring

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Question breaking string into substring

    hi there

    hi any one can tell me how could i break a string into substrings of variable length.

    example:

    string=30x+3X+21x

    substring=30,x,+,3,x,+,21,x

    thanx.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, you have three different kinds of characters:

    ¤Numbers (0,1,2...9)
    ¤Variables (Letters) (a,b,c...z)
    ¤Operators (+,-,*,/)

    1)
    Start from the beginning of the string, if it is a number or letter store it then go to 2). If its an operator, store it and goto 3).
    2)
    Search the rest of the string. If the first character is the same as you found in 1), store it and repeat from 2). If it's another kind of character, go to 3).
    3)
    Group the things you've stored in 1) and 2) into a string, then save it. Repeat from 1) with the rest of the string.

    Repeat these steps until the string is empty.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Hi Sadat,

    I currently am not having time to give u the entire program but here is the broad logic 4 it... An algorithmic approach to the solution already offered...

    1. Read a character from the input string
    2. if it is a numeric then go to step 3 else go to step 5
    3. push it onto a stack (array of characters)
    4. go to step 1
    5. if stack is not empty then go to step 6 else go to step 8
    6. convert the array of chars (string) to Int and store in the main stack (of strings- the final version of what u want)
    7. go to step 1
    8. if the read char is a symbol (+,- etc) then go to step 9 else go to step 11
    9. push it onto the final stack (strings stack)
    10. go to step 1
    11. if the read char is a char go to step 12 else go to step 14
    12. push it onto the stack (strings) (assuming that only one character appears in between, else follow logic of steps 2,3,4)
    13. go to step 1
    14. (means end of string) TERMINATE process.



    I am sure u can modify and refine the code for optimization. But thats ur job.

    Warm Regards,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM