Advertisement
joaohunter

palindromo

Sep 14th, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. typedef struct pilha{
  7.     char vet[50];
  8.     int topo;
  9. } TPilha;
  10.  
  11. void create(TPilha *p){
  12.     p->topo=-1; //pq vetor começa em zero
  13. }
  14.  
  15. void destroy(TPilha *p){
  16.     p->topo=-1;
  17. }
  18.  
  19. int isfull(TPilha *p){
  20.     //return p->topo==5; versão otimizada
  21.     if(p->topo==50)
  22.         return 1;
  23.     else
  24.         return 0;
  25. }
  26.  
  27. int isempty(TPilha *p){
  28.     //return p->topo==-1; versão otimizada
  29.     if(p->topo==-1)
  30.         return 1;
  31.     else
  32.         return 0;
  33. }
  34.  
  35. void push(TPilha *p, char x){
  36.     if(isfull(p)){
  37.         puts("overflow");
  38.         //abort();
  39.     }
  40.     p->topo++;
  41.     p->vet[p->topo]=x;
  42. }
  43.  
  44. char pop(TPilha *p){
  45.     char aux;
  46.     if(isempty(p)){
  47.         puts("underflow");
  48.         //abort();
  49.     }
  50.     aux=p->vet[p->topo];
  51.     p->topo--;
  52.     return aux;
  53. }
  54.  
  55. char top(TPilha *p){
  56.     if(isempty(p)){
  57.         puts("underflow");
  58.         //abort();
  59.     }
  60.     return p->vet[p->topo];
  61. }
  62.  
  63. void invertepal(char s[]){
  64.     int i, tam;
  65.     TPilha p;
  66.     create(&p);
  67.     tam=strlen(s);
  68.     for(i=0;i<tam;i++)
  69.         push(&p, s[i]);
  70.     for(i=0;!isempty(&p);i++)
  71.         s[i]=pop(&p);
  72.     printf("Palavra invertida: %s", s);
  73. }
  74.  
  75. int palindromo(char y[]){
  76.   int tam, i, j=0, result;
  77.   char invert[50]="";  
  78.   TPilha a;
  79.   create(&a);
  80.   tam=strlen(y);
  81.   for(i=0;i<tam;i++)
  82.     push(&a, y[i]);
  83.     for(i=0;!isempty(&a);i++)
  84.         invert[i]=pop(&a);
  85.     result=strcmp(y, invert);
  86.     if (result==0){
  87.         printf("[%s] eh igual a [%s]\n", y, invert);
  88.         return 1;
  89.   }
  90.     else{
  91.         printf("[%s] eh diferente de [%s]\n", y, invert);
  92.         return 0;
  93.   }
  94. }
  95.  
  96. int main(void){
  97.     char palavra[50], frase[50];
  98.    
  99.     printf("Digite uma palavra: ");
  100.     gets(palavra);
  101.     palindromo(palavra);
  102.    
  103.     printf("\n\nDigite uma frase: ");
  104.     gets(frase);
  105.     invertepal(frase); //zero significa igual
  106.    
  107.     getch();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement