Sai A Sai A
Updated date Feb 12, 2024
In this blog, we will learn how to convert a string into a stack in C++ with clear explanations and code examples. Explore methods using both std::stack and manual array implementation, understanding fundamental programming concepts.

Introduction:

In programming, data structures play an important role in organizing and manipulating data efficiently. One such fundamental data structure is the stack, which follows the Last In, First Out (LIFO) principle. In this blog, we will explore the conversion of a string into a stack in C++

Method 1: Using std::stack in C++

The standard template library (STL) in C++ provides a versatile container called std::stack. This container adapts the underlying container (by default, std::deque) to provide stack functionality. Let's see how we can utilize std::stack to convert a string into a stack.

#include <iostream>
#include <stack>
#include <string>

int main() {
    std::string str = "hello";
    std::stack<char> charStack;

    // Push each character of the string onto the stack
    for (char c : str) {
        charStack.push(c);
    }

    // Output the stack contents
    while (!charStack.empty()) {
        std::cout << charStack.top() << " ";
        charStack.pop();
    }

    return 0;
}

Output:

o l l e h

Here, we start by including the necessary header files. We then create a string str containing "hello" and a stack charStack to store characters. Using a loop, we push each character of the string onto the stack. Finally, we pop and output each character from the stack until it becomes empty.

Method 2: Manual Conversion Using Array

Another approach involves manually converting the string into a stack using an array. While not as elegant as utilizing std::stack, this method provides insight into the underlying mechanics of a stack.

#include <iostream>
#include <string>

#define MAX_SIZE 100

class Stack {
private:
    char data[MAX_SIZE];
    int top;

public:
    Stack() : top(-1) {}

    void push(char c) {
        if (top == MAX_SIZE - 1) {
            std::cerr << "Stack Overflow!\n";
            return;
        }
        data[++top] = c;
    }

    char pop() {
        if (top == -1) {
            std::cerr << "Stack Underflow!\n";
            return '\0';
        }
        return data[top--];
    }

    bool isEmpty() {
        return top == -1;
    }
};

int main() {
    std::string str = "hello";
    Stack charStack;

    // Push each character of the string onto the stack
    for (char c : str) {
        charStack.push(c);
    }

    // Output the stack contents
    while (!charStack.isEmpty()) {
        std::cout << charStack.pop() << " ";
    }

    return 0;
}

Output:

o l l e h

In this method, we define a Stack class with basic stack operations like push, pop, and isEmpty. We use an array data to store characters and an integer top to keep track of the top element's index. The main program then utilizes this Stack class to convert the string into a stack and output its contents.

Conclusion:

In this blog, we have discussed two methods to convert a string into a stack in C++. We first used the std::stack container provided by the STL and then manually implemented a stack using an array. Both methods yield the same output, demonstrating the flexibility and efficiency of stacks in handling data. 

Comments (0)

There are no comments. Be the first to comment!!!