w3resource

C Exercises: Reverse a string using a stack


4. String Reversal Using Stack Variants

Write a C program that accepts a string and reverse it using a stack.

Sample Solution:

C Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Global stack and top variable declaration
char stack[MAX_SIZE];
int top = -1;

// Function to push a character onto the stack
void push(char data) {
    if (top == MAX_SIZE - 1) {
        printf("Overflow stack!\n");
        return;
    }
    top++;
    stack[top] = data;
}

// Function to pop a character from the stack
char pop() {
    if (top == -1) {
        printf("Empty Stack!\n");
        return '\0';
    }
    char data = stack[top];
    top--;
    return data;
}

// Function to reverse a string using a stack
void reverse_string(char *str) {
    int len = strlen(str);

    // Push each character of the string onto the stack
    for (int i = 0; i < len; i++) {
        push(str[i]);
    }

    // Pop characters from the stack to reverse the string
    for (int i = 0; i < len; i++) {
        str[i] = pop();
    }
}

// Main function
int main() {
    char text[MAX_SIZE];
    printf("Input a string: ");
    fgets(text, MAX_SIZE, stdin);

    // Remove newline character from input
    text[strcspn(text, "\n")] = '\0';

    // Reverse the input string using the stack
    reverse_string(text);

    // Print the reversed string
    printf("Reversed string using a stack is: %s\n", text);

    return 0;
}

Output:

Input a string: w3resource
 Reversed string using a stack is: ecruoser3w

Flowchart:

Flowchart: Reverse a string using a stack.


Flowchart: Reverse a string using a stack.


For more Practice: Solve these Related Problems:

  • Write a C program to reverse the order of words in a sentence using a stack.
  • Write a C program to reverse only the vowels of a string using a stack for storing vowel indices.
  • Write a C program to check if a string remains unchanged after being reversed using stack operations.
  • Write a C program to reverse a string by simulating recursion with an explicit stack.

Go to:


PREV : Array Stack Capacity Checks.
NEXT : Dual Stack in Single Array Challenges.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.