fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. struct Stack {
  6. int capacity;
  7. char *arr;
  8. int top;
  9. };
  10.  
  11. struct Stack* createStack(unsigned capacity) {
  12. struct Stack *s = (struct Stack*)malloc(sizeof(struct Stack));
  13. s->capacity = capacity;
  14. s->top = -1;
  15. s->arr = (char*)malloc(capacity * sizeof(char));
  16. return s;
  17. };
  18.  
  19. int isFull(struct Stack *stack) {
  20. return stack->top == stack->capacity - 1;
  21. };
  22.  
  23. int isEmpty(struct Stack *stack) {
  24. return stack->top == -1;
  25. };
  26.  
  27. void push(struct Stack* stack, int data) {
  28. if(isFull(stack)) {
  29. return;
  30. }
  31. stack->arr[++stack->top] = data;
  32. }
  33.  
  34. char pop(struct Stack *stack) {
  35. if(isEmpty(stack)) {
  36. printf("isEmpty");
  37. return 0;
  38. }
  39. return stack->arr[stack->top--];
  40. }
  41.  
  42. char* reverse(char *p, int n) {
  43.  
  44. struct Stack *stack = createStack(n);
  45.  
  46. for(int i = 0; i < n; ++i) {
  47. push(stack, p[i]);
  48. }
  49.  
  50. for(int i = 0; i < n; ++i) {
  51. printf("%c", pop(stack));
  52. }
  53. };
  54.  
  55. int main(int argc, char const *argv[]) {
  56. char str[100] = "MayAnnCampanera";
  57. int n = strlen(str);
  58. reverse(str,n);
  59. return 0;
  60. }
Success #stdin #stdout 0s 5568KB
stdin
Standard input is empty
stdout
arenapmaCnnAyaM