My issue
help
My code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
struct Stack {
int top;
unsigned capacity;
char* array;
};
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
char peek(struct Stack* stack) {
return stack->array[stack->top];
}
void push(struct Stack* stack, char item) {
stack->array[++stack->top] = item;
}
char pop(struct Stack* stack) {
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
}
int main() {
char originalString[] = "Hello, World!";
int stringLength = strlen(originalString);
struct Stack* stack = createStack(MAX_SIZE);
// Push each character onto the stack
for (int i = 0; i < stringLength; i++) {
char c = originalString[i];
push(stack, c);
}
// Pop the characters from the stack to construct the reversed string
char reversedString[stringLength];
int ind = 0;
while (!isEmpty(stack)) {
reversedString[ind] = pop(stack);
ind++;
}
reversedString[ind] = '\0'; // Null-terminate the reversed string
printf("%s\n", reversedString);
// Free the dynamically allocated memory for the stack
free(stack->array);
free(stack);
return 0;
}
Learning course: BCS304: Data structures and Applications
Problem Link: Infix to Postfix in BCS304: Data structures and Applications