Herowaree

Palindromo_ENC.c

Nov 29th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct No{
  6. char dado;
  7. struct No *proximo;
  8. };
  9.  
  10. struct Pilha{
  11. int tamanho;
  12. struct No *cabeca;
  13. };
  14.  
  15. void iniciaPilha(struct Pilha *pilha){
  16. pilha->tamanho = 0;
  17. pilha->cabeca = NULL;
  18. }
  19.  
  20. int tamanhoPilha(struct Pilha *pilha){
  21. return pilha->tamanho;
  22. }
  23.  
  24. int vaziaPilha(struct Pilha *pilha){
  25. if(pilha->tamanho == 0){
  26. return 1;
  27. }else{
  28. return 0;
  29. }
  30. }
  31.  
  32. char topoPilha(struct Pilha *pilha){
  33. if(vaziaPilha(pilha)) return '0';
  34.  
  35. return (pilha->cabeca)->dado;
  36. }
  37.  
  38. int push(struct Pilha *pilha, char caracter){
  39. struct No *novoNo;
  40. novoNo = malloc(sizeof(struct No));
  41.  
  42. if(novoNo == NULL) return 0;
  43.  
  44. novoNo->dado = caracter;
  45. novoNo->proximo = pilha->cabeca;
  46. pilha->cabeca = novoNo;
  47.  
  48. pilha->tamanho++;
  49.  
  50. return 1;
  51. }
  52.  
  53. char pop(struct Pilha *pilha){
  54. struct No *aux = pilha->cabeca;
  55. char caracter;
  56. if(vaziaPilha(pilha)) return '0';
  57.  
  58. caracter = (pilha->cabeca)->dado;
  59. pilha->cabeca = aux->proximo;
  60. free(aux);
  61. pilha->tamanho--;
  62.  
  63. return caracter;
  64. }
  65.  
  66. int main(){
  67. char string[1001], string_invertida[1001];
  68.  
  69. struct Pilha pilha;
  70. iniciaPilha(&pilha);
  71.  
  72. scanf("%1000[^\n]", string);
  73.  
  74. int i, tam = strlen(string);
  75. for(i = 0; i < tam; i++){
  76. push(&pilha, string[i]);
  77. }
  78.  
  79. for(i = 0; i < tam; i++){
  80. string_invertida[i] = pop(&pilha);
  81. }
  82. string_invertida[i] = '\0';
  83.  
  84. if(strcmp(string, string_invertida) == 0){
  85. printf("A palavra/frase %s é palindromo.\n", string);
  86. }else{
  87. printf("A palavra/frase %s não é palindromo.\n", string);
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment