Advertisement
pablosoares

Untitled

Sep 7th, 2020
1,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. struct stack{
  7.    
  8.     char vet[200];
  9.     int top;
  10.    
  11.     }typedef node;
  12.    
  13. void iniciar(node *p) {
  14.     p->top=-1;
  15. }
  16.  
  17. void push(node *p,char x){
  18.      
  19.     if (p->top==199) printf("Maximo de caracteres.\n");
  20.     else{
  21.         p->top++;
  22.         p->vet[p->top]=x;
  23.     }
  24.    
  25.     }
  26.    
  27.    
  28. char pop(node *p){
  29.    
  30.     char x;
  31.    
  32.     if(p->top==-1)return 0;
  33.        
  34.     x=p->vet[p->top];
  35.     p->top--;
  36.    
  37.     return x;
  38.      
  39.     }
  40.  
  41. void inverte(char fr[],node *p){
  42.    
  43.     int i=0,cont=0,k=0,j=0,x=0;
  44.     char aux[200];
  45.    
  46.     while(fr[i]!='\0'){
  47.         if(fr[i]==' '||i==0)cont++;
  48.         i++;
  49.     }
  50.    
  51.     cont/=2;
  52.    
  53.     if(cont%2==1)cont++;
  54.    
  55.     i=0;
  56.    
  57.     while(cont--){
  58.          
  59.          while(fr[i]!='\0'&&fr[i]!=' '&&fr[i]!=','){
  60.              
  61.              push(p,fr[i]);
  62.              i++;
  63.              
  64.              }
  65.              
  66.             if(fr[i]==','){
  67.                
  68.                aux[k]=fr[i];
  69.                k++;
  70.                i++;
  71.              
  72.               }
  73.          
  74.                aux[k]=fr[i];
  75.                k++;
  76.                i++;
  77.                
  78.           while(fr[i]!='\0'&&fr[i]!=' '){
  79.              
  80.               aux[k]=fr[i];
  81.               k++;
  82.               i++;
  83.              
  84.               }
  85.              
  86.             aux[k]=fr[i];
  87.            
  88.             k++;
  89.             i++;
  90.            
  91.             aux[k]='\0';
  92.            
  93.          while(p->top!=-1){
  94.              
  95.               fr[j]=pop(p);
  96.               j++;
  97.                          
  98.              }
  99.              
  100.           while(aux[x]!='\0'){
  101.              
  102.               fr[j]=aux[x];
  103.               x++;
  104.               j++;
  105.              
  106.               }
  107.                
  108.  
  109.         }                
  110.    
  111.     }
  112.    
  113. int main(){
  114.    
  115.    node pilha;
  116.    iniciar(&pilha);
  117.    
  118.    char pa[200];
  119.    gets(pa);
  120.    
  121.    inverte(pa,&pilha);
  122.    puts(pa);
  123.    
  124.    //HOJE O SOL ESTA LINDO, ILUMINANDO NOSSAS VIDAS
  125. return 0;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement