Strings & Stacks

This is a discussion on Strings & Stacks within the C++ Programming forums, part of the General Programming Boards category; Still working with stacks and have gotten use to working with them but i have encountered another problem. I have ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    Strings & Stacks

    Still working with stacks and have gotten use to working with them but i have encountered another problem. I have to input an expression as a string and push the numbers in it on the stack (example: 54 - 34 + 4 * 94 ) What I cant figure out is how to push the things onto the stack properly. If I push everything on there character by character it will push 5 then 4 on there instead of pushing 54. How can I tell it to that each item to be pushed is seperated by a space....

  2. #2
    Unregistered
    Guest
    parse the entire string into substrings using space as delimiter. this can be done using either a user defined function or strtok(). Each substring can then be dealt with individually, converted to numerical value if indicated, etc.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Thats what I thought I woudl have to do somehow but am unclear on how to do it. Can you explain more on the function you mentioned ( strtok(). ) I have never heard of it. I had considered doing my own function to do this but am not sure exactly how to go about it. I know I want to read the string until I get to a space then push everything before that space onto the stack and continue on but am not sure how to code this. ANy suggestions?

  4. #4
    Unregistered
    Guest
    you should be able to find strtok() by looking in the help section of your compiler or doing a search of this board. I don't remember the details myself but basically you declare a string that contains whatever you want to use as a delimiter:

    //delimitiers will be commas, spaces, or colons
    char delimiters[] = ", :";

    and pass the string to tokenize (a token is each of the substrings)

    //string to tokenize
    char string[] = "54 + 6 * 3";

    and the delimiters to strtok() with return assigned to a char *:

    //find first token
    char * ptr = strtok(string, delimeters);

    //now store or somehow save the value of ptr somewhere before you move on to the next token.

    my problem is remembering from here. To get the rest of the tokens I think you do something like this:

    ptr = strtok(NULL, delimeters);

    in a loop somehow, but I just don't remember. I believe that in the process of tokenizing you end up destroying the original string so use of copy of the original if you need the original for some purpose later.

    -------------------------------------

    To make my own "tokenizer" I usually scan the string char by char looking for the delimiter, storing each char in a char array until if find the delimiter, then adding null terminating char to the char array before going on to the next char.

    int i, j, k;
    //an array of 10 tokens each up to 9 char long.
    char tokens[10][10];

    //tokenizing algorhythm for just a space as delimiter
    for(i = 0, j = 0, k = 0; i < strlen(string) && j < 10 && k < 10; i++)
    {
    if(string[i] != ' ')
    {
    tokens[j][k++] = string[i];
    }
    else if(string[i] == ' ')
    {
    tokens[j++][k] = '\0';
    k = 0;
    }//end else if
    }//end for

    You should be aware that none of the code written above is tested/compiled as I don't have a compiler available.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Got a small program. To Run, here is the code.

    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <stdio.h>
    #include <stack>
    using namespace std;


    char expression[]="43 42 41 + 4";
    char seps[] =" ";
    char *ptr;

    void main()
    { //Opens Main
    ptr = strtok(expression, seps);
    cout<<ptr;

    while (ptr !=NULL);
    {
    ptr = strtok(NULL, seps);
    cout<<ptr;
    }


    getch();
    }

    Only problem is that when I run it to see the output all I get is 43. Why isn't it showing the rest of the numbers.
    Last edited by AdioKIP; 03-07-2002 at 08:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  5. Stacks & Strings
    By AdioKIP in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2002, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21