|
1 | 1 | #include <stdio.h>
|
2 |
| -#include <stdlib.h> |
3 | 2 | #include <string.h>
|
4 | 3 |
|
5 |
| -/* |
6 |
| -* Problema do palindromo |
7 |
| -* Desenvolvido por Igor Alves (https://github.com/iguit0) |
8 |
| -*/ |
| 4 | +#define TAMANHO_MAX_PALAVRA 100 |
9 | 5 |
|
10 |
| -typedef struct sCell{ |
11 |
| - char info; |
12 |
| - struct sCell* next; |
13 |
| -}CELULA; |
| 6 | +void calc_reverse(char *input, char *output) { |
| 7 | + size_t len_input = strlen(input); |
14 | 8 |
|
15 |
| -typedef struct sPilha{ |
16 |
| - CELULA* topo; |
17 |
| -}PILHA; |
| 9 | + char *last_char = input + len_input - 1; |
18 | 10 |
|
19 |
| -CELULA* criarCelula(){ |
20 |
| - CELULA* nova; |
21 |
| - nova = (CELULA *) malloc(sizeof(CELULA)); |
22 |
| - return nova; |
| 11 | + for (int i=0; i < len_input; i++) { |
| 12 | + output[i] = *(last_char - i); |
| 13 | + } |
| 14 | + output[len_input] = '\0'; |
23 | 15 | }
|
24 | 16 |
|
25 |
| -void inicializar(PILHA* p){ |
26 |
| - p->topo = NULL; |
27 |
| -} |
| 17 | +int main() { |
| 18 | + char input[TAMANHO_MAX_PALAVRA]; |
| 19 | + char reverse[TAMANHO_MAX_PALAVRA]; |
28 | 20 |
|
29 |
| -int pilhaVazia(PILHA* p){ |
30 |
| - if(p->topo == NULL) return 1; |
31 |
| - else return 0; |
32 |
| -} |
| 21 | + printf("Digite uma palavra: "); |
33 | 22 |
|
34 |
| -int push(PILHA* p,char elemento){ |
35 |
| - CELULA* nova = criarCelula(); |
36 |
| - if(nova == NULL) return 0; |
37 |
| - nova->info = elemento; |
38 |
| - nova->next = p->topo; |
39 |
| - if(pilhaVazia(p)){ |
40 |
| - p->topo = nova; |
41 |
| - return 1; |
42 |
| - } |
43 |
| - p->topo = nova; |
44 |
| - return 1; |
45 |
| -} |
| 23 | + fgets(input, TAMANHO_MAX_PALAVRA, stdin); |
| 24 | + //remove New Line from the end; |
| 25 | + input[strlen(input) - 1] = '\0'; |
46 | 26 |
|
47 |
| -char pop(PILHA* p){ |
48 |
| - CELULA* removida; |
49 |
| - char removido; |
50 |
| - if(pilhaVazia(p)){ |
51 |
| - printf("\nPilha Vazia!"); |
52 |
| - return removido; |
53 |
| - } |
54 |
| - removida = p->topo; |
55 |
| - removido = removida->info; |
56 |
| - p->topo = p->topo->next; |
57 |
| - free(removida); |
58 |
| - return removido; |
59 |
| -} |
| 27 | + calc_reverse(input, reverse); |
60 | 28 |
|
61 |
| -int palindromo(PILHA* p){ |
62 |
| - if(pilhaVazia(p)){ |
63 |
| - printf("Pilha Vazia!\n"); |
64 |
| - return 0; |
65 |
| - } |
66 |
| - PILHA p2; |
67 |
| - inicializar(&p2); |
68 |
| - PILHA p3; |
69 |
| - inicializar(&p3); |
70 |
| - char aux; |
71 |
| - char aux2; |
72 |
| - while(!pilhaVazia(p)){ |
73 |
| - aux = pop(p); |
74 |
| - if(aux != '.' && aux != ' '){ |
75 |
| - push(&p2,aux); |
76 |
| - } |
77 |
| - } |
78 |
| - while(!pilhaVazia(&p2)){ |
79 |
| - aux = pop(&p2); |
80 |
| - push(p,aux); |
81 |
| - push(&p3,aux); |
82 |
| - } |
83 |
| - while(!pilhaVazia(&p3)){ |
84 |
| - aux = pop(&p3); |
85 |
| - push(&p2,aux); |
86 |
| - } |
87 |
| - while(!pilhaVazia(p)){ |
88 |
| - aux = pop(p); |
89 |
| - aux2 = pop(&p2); |
90 |
| - if(aux != aux2){ |
91 |
| - return 0; |
92 |
| - } |
93 |
| - } |
94 |
| - return 1; |
95 |
| -} |
96 |
| - |
97 |
| -void imprimirInverso(PILHA* p){ |
98 |
| - PILHA p2; |
99 |
| - inicializar(&p2); |
100 |
| - char aux; |
101 |
| - if(pilhaVazia(p)) return; |
102 |
| - while(!pilhaVazia(p)){ |
103 |
| - aux = pop(p); |
104 |
| - putchar(aux); |
105 |
| - push(&p2,aux); |
106 |
| - } |
107 |
| - while(!pilhaVazia(&p2)) |
108 |
| - push(p,pop(&p2)); |
109 |
| - |
110 |
| -} |
| 29 | + printf("Sua palavra invertida: %s\n", reverse); |
111 | 30 |
|
112 |
| -int main(){ |
113 |
| - PILHA p; |
114 |
| - inicializar(&p); |
115 |
| - int i=0; |
116 |
| - char palavra[100]; |
117 |
| - char inverso[100]; |
118 |
| - printf("\nPalavra: "); |
119 |
| - scanf("%s",palavra); |
120 |
| - |
121 |
| - for(i=0;i<strlen(palavra);i++){ |
122 |
| - push(&p,palavra[i]); |
123 |
| - } |
124 |
| - |
125 |
| - printf("\nImprimindo inverso > "); |
126 |
| - imprimirInverso(&p); |
127 |
| - putchar('\n'); |
128 |
| - |
129 |
| - if(palindromo(&p)==1) printf("\nPalindromo"); |
130 |
| - else printf("\nNao eh palindromo"); |
| 31 | + if (!strcmp(input, reverse)) |
| 32 | + puts("Sua palavra eh um palindromo!"); |
| 33 | + else |
| 34 | + puts("Sua palavra NAO eh um palindromo!"); |
131 | 35 |
|
132 |
| - return 0; |
| 36 | + return 0; |
133 | 37 | }
|
0 commit comments