Advertisement
Jvsierra

Misturador de texto em C

Jun 19th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7.  
  8. #define LIM_TXT 20000
  9.  
  10. char texto[LIM_TXT];
  11. int comecoPalavra = 0, fimPalavra = 0, achouPalavra;
  12.  
  13. bool eLetra(char letra);
  14.  
  15. bool eCaractereConsideravel(char caractere);
  16.  
  17. void embaralha();
  18.  
  19. void achaPalavra(int comeco);
  20.  
  21. int main()
  22. {
  23.     srand(time(NULL));
  24.    
  25.     int i;
  26.    
  27.     printf("Digite o texto:\n");
  28.     fflush(stdin);
  29.     gets(texto);
  30.    
  31.     embaralha();
  32.    
  33.     puts(texto);
  34.    
  35.     getch();
  36. }
  37.  
  38. bool eLetra(char letra)
  39. {
  40.     if( ( (int)letra >= 65 && (int)letra <= 90) || ( (int)letra >= 97 && (int)letra <= 122))
  41.         return true;
  42.     else
  43.         return false;
  44. }
  45.  
  46. void embaralha()
  47. {
  48.     int i, tlPosicoes, posicoes[LIM_TXT], pos1, pos2;
  49.     char aux;
  50.    
  51.     i = 0;
  52.    
  53.     while(i <= strlen(texto))
  54.     {
  55.         achaPalavra(i);
  56.        
  57.         if(achouPalavra == 1)
  58.         {
  59.             if(fimPalavra - comecoPalavra > 2)
  60.             {
  61.                 tlPosicoes = 0;
  62.                
  63.                 for(i = comecoPalavra + 1; i <= fimPalavra - 1; i++)
  64.                 {
  65.                     posicoes[tlPosicoes] = i;
  66.                    
  67.                     tlPosicoes++;
  68.                 }
  69.                
  70.                 while(tlPosicoes > 1)
  71.                 {
  72.                     do
  73.                     {
  74.                         pos1 = rand() % tlPosicoes;
  75.                         pos2 = rand() % tlPosicoes;
  76.                     }while(pos1 == pos2);
  77.                    
  78.                     aux = texto[posicoes[pos1]];
  79.                     texto[posicoes[pos1]] = texto[posicoes[pos2]];
  80.                     texto[posicoes[pos2]] = aux;
  81.                    
  82.                     for(i = pos1; i < tlPosicoes; i++)
  83.                         posicoes[i] = posicoes[i + 1];
  84.                        
  85.                     tlPosicoes--;
  86.        
  87.                     for(i = pos2; i < tlPosicoes; i++)
  88.                         posicoes[i] = posicoes[i + 1];
  89.                        
  90.                     tlPosicoes--;
  91.                    
  92.                 }
  93.             }
  94.            
  95.             i = fimPalavra + 2;
  96.         }
  97.         else
  98.             i++;
  99.     }
  100. }
  101.  
  102. void achaPalavra(int comeco)
  103. {
  104.     int i, j;
  105.    
  106.     achouPalavra = 0;
  107.  
  108.     for(i = comeco; achouPalavra == 0 && i <= strlen(texto); i++)
  109.     {
  110.         if((texto[i] == ' ' || texto[i] == '\0' || texto[i] == 10 || eCaractereConsideravel(texto[i])) && toupper(texto[i - 1]) >= 'A' && toupper(texto[i - 1]) <= 'Z')
  111.         {
  112.             fimPalavra = i - 1;
  113.            
  114.             for(j = i - 1; eLetra(texto[j - 1]); j--)  
  115.                 comecoPalavra = j;
  116.                
  117.             comecoPalavra = j;
  118.             achouPalavra = 1;
  119.         }
  120.     }
  121. }
  122.  
  123. bool eCaractereConsideravel(char caractere)
  124. {
  125.     if((caractere >= 33 && caractere <= 64) || (caractere >= 91 && caractere <= 96) || (caractere >= 123 && caractere <= 126))
  126.         return true;
  127.     else
  128.         return false;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement