fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define STACK_SIZE 10
  5. #define STACK_EMPTY -1
  6. void push(char [], // input/ouput - the stack
  7. char, // input - data being pushed onto the stack
  8. int *, // input/output - pointer to the index of the top of stack
  9. int); // constant - maximum size of stack
  10. char // output - data being popped out from the stack
  11. pop(char [], // input/output - the stack
  12. int *); // input/output - pointer to the index of the top of stack
  13. void push(char stack[],char item,int *top,int max_size){
  14. stack[++(*top)] = item;
  15. }
  16. char pop(char stack[],int *top){
  17. return stack[(*top)--];
  18. }
  19. int main(){
  20. char s[STACK_SIZE];
  21. int s_top = STACK_EMPTY; // Pointer points to the index of the top of the stack
  22.  
  23. char randChar = ' ';
  24. int i = 0;
  25. int j=0;
  26. int randNum = 0;
  27.  
  28. srand(time(NULL));
  29.  
  30. for (i = 0; i < STACK_SIZE; i++){
  31. randNum = 33 + (int)(rand() % ((126-33)+ 1 ));
  32. randChar = (char) randNum;
  33. push(s,randChar, &s_top, STACK_SIZE);
  34.  
  35. printf ("Random char: %c\n", randChar);
  36.  
  37. }
  38. printf("-----------\n");
  39.  
  40. for(j=STACK_SIZE; j>0; j--){
  41. printf("Random chars:%c\n", pop(s, &s_top));
  42. }
  43. return 0;
  44. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Random char: {
Random char: !
Random char: (
Random char: A
Random char: P
Random char: L
Random char: \
Random char: 1
Random char: A
Random char: ^
-----------
Random chars:^
Random chars:A
Random chars:1
Random chars:\
Random chars:L
Random chars:P
Random chars:A
Random chars:(
Random chars:!
Random chars:{