Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions C/Palindromo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
* Problema do palindromo
* Desenvolvido por Igor Alves (https://github.com/iguit0)
*/

typedef struct sCell{
char info;
struct sCell* next;
}CELULA;

typedef struct sPilha{
CELULA* topo;
}PILHA;

CELULA* criarCelula(){
CELULA* nova;
nova = (CELULA *) malloc(sizeof(CELULA));
return nova;
}

void inicializar(PILHA* p){
p->topo = NULL;
}

int pilhaVazia(PILHA* p){
if(p->topo == NULL) return 1;
else return 0;
}

int push(PILHA* p,char elemento){
CELULA* nova = criarCelula();
if(nova == NULL) return 0;
nova->info = elemento;
nova->next = p->topo;
if(pilhaVazia(p)){
p->topo = nova;
return 1;
}
p->topo = nova;
return 1;
}

char pop(PILHA* p){
CELULA* removida;
char removido;
if(pilhaVazia(p)){
printf("\nPilha Vazia!");
return removido;
}
removida = p->topo;
removido = removida->info;
p->topo = p->topo->next;
free(removida);
return removido;
}

int palindromo(PILHA* p){
if(pilhaVazia(p)){
printf("Pilha Vazia!\n");
return 0;
}
PILHA p2;
inicializar(&p2);
PILHA p3;
inicializar(&p3);
char aux;
char aux2;
while(!pilhaVazia(p)){
aux = pop(p);
if(aux != '.' && aux != ' '){
push(&p2,aux);
}
}
while(!pilhaVazia(&p2)){
aux = pop(&p2);
push(p,aux);
push(&p3,aux);
}
while(!pilhaVazia(&p3)){
aux = pop(&p3);
push(&p2,aux);
}
while(!pilhaVazia(p)){
aux = pop(p);
aux2 = pop(&p2);
if(aux != aux2){
return 0;
}
}
return 1;
}

void imprimirInverso(PILHA* p){
PILHA p2;
inicializar(&p2);
char aux;
if(pilhaVazia(p)) return;
while(!pilhaVazia(p)){
aux = pop(p);
putchar(aux);
push(&p2,aux);
}
while(!pilhaVazia(&p2))
push(p,pop(&p2));

}

int main(){
PILHA p;
inicializar(&p);
int i=0;
char palavra[100];
char inverso[100];
printf("\nPalavra: ");
scanf("%s",palavra);

for(i=0;i<strlen(palavra);i++){
push(&p,palavra[i]);
}

printf("\nImprimindo inverso > ");
imprimirInverso(&p);
putchar('\n');

if(palindromo(&p)==1) printf("\nPalindromo");
else printf("\nNao eh palindromo");

return 0;
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Algoritmos em C/C++, Java, Python, Go e Ruby desenvolvidos como estudo de Algori
|----|-------------------------------------|-------|------|--------|----|------|
| 49 | Lista com 2 Pilhas | C/C++ | Java | [Python](/Python/ListaComPilhas.py) | Go | Ruby |
| 50 | Problema da Soma de 2 Números | C/C++ | Java | [Python](/Python/Soma2Numeros.py) | Go | Ruby |
| 51 | [Palíndromo][49] | [C/C++](/C/Palindromo.c) | Java | Python | Go | Ruby |

Quem tiver interesse em colaborar basta enviar um pull request com seu algoritmo/correção.

Expand Down Expand Up @@ -106,3 +107,4 @@ Quem tiver interesse em colaborar basta enviar um pull request com seu algoritmo
[46]: https://pt.wikipedia.org/wiki/Radix_sort
[47]: https://pt.wikipedia.org/wiki/Selection_sort
[48]: https://pt.wikipedia.org/wiki/Shell_sort
[49]: https://pt.wikipedia.org/wiki/Pal%C3%ADndromo