Answer to Question #276191 in C for MRM

Question #276191

Write a program that reads in a sequence of characters and prints them in reverse order (Use a stack).


1
Expert's answer
2021-12-10T00:36:56-0500
#include <stdio.h>
#include <string.h>

char stack[1024];
int size = 0;

void push(char c) {
  size++;
  stack[size] = c;
}

char pop() {
  char c = stack[size];
  size--;
  return c;
}

int main(int argc, char **argv) {
  (void) argc; (void) argv;

  char seq[1024];
  printf("Enter a sequence of chars: ");
  scanf("%s", &seq);
  for (int i = 0; i < strlen(seq); i++) {
    push(seq[i]);
  }
  while (size != 0) {
    printf("%c", pop());
  }
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS