fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX 100 /*maximum no. of characters*/
  5.  
  6. /*stack variables*/
  7. int top=-1;
  8. int item;
  9. /***************/
  10.  
  11. /*string declaration*/
  12. char stack_string[MAX];
  13.  
  14. /*function to push character (item)*/
  15. void pushChar(char item);
  16.  
  17. /*function to pop character (item)*/
  18. char popChar(void);
  19.  
  20. /*function to check stack is empty or not*/
  21. int isEmpty(void);
  22.  
  23. /*function to check stack is full or not*/
  24. int isFull(void);
  25.  
  26. int main()
  27. {
  28. char str[MAX];
  29.  
  30. int i;
  31.  
  32. printf("Input a string: ");
  33. scanf("%[^\n]s",str); /*read string with spaces*/
  34. /*gets(str);-can be used to read string with spaces*/
  35.  
  36. for(i=0;i<strlen(str);i++)
  37. pushChar(str[i]);
  38.  
  39. for(i=0;i<strlen(str);i++)
  40. str[i]=popChar();
  41.  
  42. printf("Reversed String is: %s\n",str);
  43.  
  44. return 0;
  45. }
  46.  
  47. /*function definition of pushChar*/
  48. void pushChar(char item)
  49. {
  50. /*check for full*/
  51. if(isFull())
  52. {
  53. printf("\nStack is FULL !!!\n");
  54. return;
  55. }
  56.  
  57. /*increase top and push item in stack*/
  58. top=top+1;
  59. stack_string[top]=item;
  60. }
  61.  
  62. /*function definition of popChar*/
  63. char popChar()
  64. {
  65. /*check for empty*/
  66. if(isEmpty())
  67. {
  68. printf("\nStack is EMPTY!!!\n");
  69. return 0;
  70. }
  71.  
  72. /*pop item and decrease top*/
  73. item = stack_string[top];
  74. top=top-1;
  75. return item;
  76. }
  77.  
  78. /*function definition of isEmpty*/
  79. int isEmpty()
  80. {
  81. if(top==-1)
  82. return 1;
  83. else
  84. return 0;
  85. }
  86.  
  87. /*function definition of isFull*/
  88. int isFull()
  89. {
  90. if(top==MAX-1)
  91. return 1;
  92. else
  93. return 0;
  94. }
Success #stdin #stdout 0s 5480KB
stdin
Standard input is empty
stdout
Input a string: Reversed String is: