Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<ctype.h>
- typedef struct pilha{
- char vet[50];
- int topo;
- } TPilha;
- void create(TPilha *p){
- p->topo=-1; //pq vetor começa em zero
- }
- void destroy(TPilha *p){
- p->topo=-1;
- }
- int isfull(TPilha *p){
- //return p->topo==5; versão otimizada
- if(p->topo==50)
- return 1;
- else
- return 0;
- }
- int isempty(TPilha *p){
- //return p->topo==-1; versão otimizada
- if(p->topo==-1)
- return 1;
- else
- return 0;
- }
- void push(TPilha *p, char x){
- if(isfull(p)){
- puts("overflow");
- //abort();
- }
- p->topo++;
- p->vet[p->topo]=x;
- }
- char pop(TPilha *p){
- char aux;
- if(isempty(p)){
- puts("underflow");
- //abort();
- }
- aux=p->vet[p->topo];
- p->topo--;
- return aux;
- }
- char top(TPilha *p){
- if(isempty(p)){
- puts("underflow");
- //abort();
- }
- return p->vet[p->topo];
- }
- void invertepal(char s[]){
- int i, tam;
- TPilha p;
- create(&p);
- tam=strlen(s);
- for(i=0;i<tam;i++)
- push(&p, s[i]);
- for(i=0;!isempty(&p);i++)
- s[i]=pop(&p);
- printf("Palavra invertida: %s", s);
- }
- int palindromo(char y[]){
- int tam, i, j=0, result;
- char invert[50]="";
- TPilha a;
- create(&a);
- tam=strlen(y);
- for(i=0;i<tam;i++)
- push(&a, y[i]);
- for(i=0;!isempty(&a);i++)
- invert[i]=pop(&a);
- result=strcmp(y, invert);
- if (result==0){
- printf("[%s] eh igual a [%s]\n", y, invert);
- return 1;
- }
- else{
- printf("[%s] eh diferente de [%s]\n", y, invert);
- return 0;
- }
- }
- int main(void){
- char palavra[50], frase[50];
- printf("Digite uma palavra: ");
- gets(palavra);
- palindromo(palavra);
- printf("\n\nDigite uma frase: ");
- gets(frase);
- invertepal(frase); //zero significa igual
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement