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.
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.
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.
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,