Write a program that reads in a sequence of characters and prints them in reverse order (Use a stack).
#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;
}
Comments
Leave a comment