fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. int top=-1;
  6.  
  7. char stack_string[100];
  8. void push(char word);
  9. char pop(void);
  10.  
  11. int main(void){
  12. char input;
  13. char str[100];
  14. int i, len;
  15. printf("press [a] for palindrome check, [b] for string reversal:");
  16. scanf("%c", &input);
  17. printf("Input a string: ");
  18. scanf(" %99[^\n]", str);
  19. len = strlen(str);
  20.  
  21. if (input == 'b'){
  22. for(i = 0; i < len; i++)
  23. push(str[i]);
  24. for(i = 0; i < len; i++)
  25. str[i]=pop();
  26. printf("Reversed String is: %s\n", str);
  27. }
  28. else if(input == 'a'){
  29. int count = 0;
  30.  
  31. for (i = 0; i < len; i++){
  32. if(!isspace((unsigned char)str[i]))
  33. push(str[i]);
  34. }
  35.  
  36. for (i = 0; i < len; i++){
  37. if (isspace((unsigned char)str[i]) || tolower(str[i])==tolower(pop()))
  38. count++;
  39. }
  40.  
  41. if (count == len)
  42. printf("%s is a palindrome\n", str);
  43. else
  44. printf("%s is not a palindrome\n", str);
  45. }
  46. return 0;
  47. }
  48.  
  49. void push(char word){
  50. top=top+1;
  51. stack_string[top]=word;
  52. }
  53.  
  54. char pop(){
  55. char word = stack_string[top];
  56. top=top-1;
  57. return word;
  58. }
Success #stdin #stdout 0s 9432KB
stdin
a
MIU im
stdout
press [a] for palindrome check, [b] for string reversal:Input a string: MIU im is a palindrome