Advertisement
Jvsierra

Primeira Lista Strings

Sep 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio2.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define LIM 101
  7.  
  8. void NumVogais(void)
  9. {
  10.     int i, cont = 0;
  11.     char str[LIM];
  12.    
  13.     clrscr();
  14.    
  15.     printf("Digite uma string:\n");
  16.     fflush(stdin);
  17.     gets(str);
  18.    
  19.     for(i = 0; i < strlen(str); i++)
  20.         if(toupper(str[i]) == 'A' ||
  21.         toupper(str[i]) == 'E' ||
  22.         toupper(str[i]) == 'I' ||
  23.         toupper(str[i]) == 'O' ||
  24.         toupper(str[i]) == 'U' )
  25.             cont++;
  26.            
  27.     printf("%d vogais na frase\n", cont);          
  28.            
  29.     getch();
  30. }
  31.  
  32. void Formata(void)
  33. {
  34.     char str[LIM];
  35.     int i;
  36.    
  37.     clrscr();
  38.    
  39.     printf("Digite um texto:\n");
  40.     fflush(stdin);
  41.     gets(str);
  42.    
  43.     str[0] = toupper(str[0]);
  44.    
  45.     for(i = 1; i < strlen(str); i++)
  46.         if(toupper(str[i]) >= 65 && toupper(str[i]) <= 92)
  47.         {
  48.            
  49.             if(str[i - 1] == ' ')
  50.                 str[i] = toupper(str[i]);
  51.             else
  52.                 str[i] = tolower(str[i]);
  53.         }
  54.            
  55.     printf("%s\n", str);
  56.    
  57.     getch();
  58. }
  59.  
  60. void ContaPalavras(void)
  61. {
  62.     int i, cont = 0;
  63.     char str[LIM];
  64.     clrscr();
  65.    
  66.     printf("Digite uma string:\n");
  67.     fflush(stdin);
  68.     gets(str);
  69.    
  70.     if(toupper(str[0]) >= 65 && toupper(str[0]) <= 92)
  71.         cont++;
  72.        
  73.     for(i = 1; i < strlen(str); i++)
  74.         if(toupper(str[i]) >= 65 && toupper(str[i]) <= 92)
  75.             if(str[i - 1] == ' ')
  76.                 cont++;
  77.                
  78.     printf("%d palavras\n", cont);
  79.    
  80.     getch();
  81. }
  82. void Duplica(void)
  83. {
  84.     int i, j;
  85.     char str[LIM];
  86.    
  87.     clrscr();
  88.    
  89.     printf("Digite o texto:\n");
  90.     fflush(stdin);
  91.     gets(str);
  92.    
  93.     for(i = 0; i < strlen(str); i += 2)
  94.     {
  95.         if(toupper(str[i]) >= 65 && toupper(str[i]) <= 92)
  96.         {
  97.             for(j = strlen(str); j > i; j--)
  98.             str[j + 1] = str[j];
  99.            
  100.             str[i + 1] = str[i];
  101.         }
  102.     }
  103.    
  104.     printf("%s\n", str);
  105.    
  106.     getch();
  107. }
  108.  
  109. void ContaLetrasDif(void)
  110. {
  111.     int i, cont, j, pos, tot = 0;
  112.     char str[20];
  113.    
  114.     clrscr();
  115.    
  116.     printf("Digite uma string:\n");
  117.     fflush(stdin);
  118.     gets(str);
  119.    
  120.     for(i = 0; i < strlen(str); i++)
  121.     {
  122.         pos = 0;
  123.        
  124.         cont = 0;
  125.        
  126.         while(pos < strlen(str))
  127.         {
  128.             if(toupper(str[i]) == toupper(str[pos]))
  129.                 cont++;
  130.                
  131.             pos++;
  132.         }
  133.                
  134.         if(cont == 1)
  135.             tot++;
  136.     }
  137.    
  138.     printf("%d letras diferentes\n");
  139.    
  140.     getch();
  141. }
  142.  
  143. void Palindromo(void)
  144. {
  145.     char str[LIM], aux[LIM];
  146.     int i, j, k, TLP = 0;
  147.    
  148.     clrscr();
  149.    
  150.     printf("Digite uma string:\n");
  151.     fflush(stdin);
  152.     gets(str);
  153.    
  154.     for(i = 0; i <= strlen(str); i++)
  155.     {
  156.         if(toupper(str[i]) >= 65 && toupper(str[i]) <= 92)
  157.             aux[TLP++] = str[i];
  158.         else if(str[i] == ' ' || str[i] == '\0')
  159.         {
  160.             aux[TLP] = '\0';
  161.            
  162.             for(j = 0, k = strlen(aux) - 1; str[j] == str[k] && j <= k; j++, k--)
  163.                 ;
  164.                
  165.             if(j > k)
  166.                 printf("%s e palindromo\n", aux);
  167.                
  168.             TLP = 0;
  169.         }
  170.     }
  171.    
  172.     getch();
  173. }
  174.  
  175. void TiraEspacos(void)
  176. {
  177.     char Entrada[LIM], Saida[LIM];
  178.     int i, pos = 0;
  179.    
  180.     clrscr();
  181.    
  182.     printf("Digite o texto:\n");
  183.     fflush(stdin);
  184.     gets(Entrada);
  185.    
  186.     for(i = 0; i < strlen(Entrada); i++)
  187.         if(Entrada[i] != ' ')
  188.             Saida[pos++] = Entrada[i];
  189.            
  190.     Saida[pos] = '\0';
  191.    
  192.     printf("\n%s\n", Saida);
  193.  
  194.     getch();
  195. }
  196.  
  197. char Menu(void)
  198. {
  199.     clrscr();
  200.    
  201.     printf("Digite a opcao desejada ou X para sair:\n");
  202.    
  203.     printf("[1] - Exercicio 1\n");
  204.     printf("[2] - Exercicio 2\n");
  205.     printf("[3] - Exercicio 3\n");
  206.     printf("[4] - Exercicio 4\n");
  207.     printf("[5] - Exercicio 5\n");
  208.     printf("[6] - Exercicio 6\n");
  209.     printf("[7] - Exercicio 7\n");
  210.     printf("[8] - Exercicio 8\n");
  211.     return toupper(getche());
  212. }
  213.  
  214. void Executa(void)
  215. {
  216.     char Op;
  217.    
  218.     Op = Menu();
  219.    
  220.     while(Op != 'X')
  221.     {
  222.         switch(Op)
  223.         {
  224.             case '1':
  225.                 NumVogais();
  226.             break;
  227.             case '2':
  228.                
  229.             break;
  230.             case '3':
  231.                 Formata();
  232.             break;
  233.             case '4':
  234.                 ContaPalavras();
  235.             break;
  236.             case '5':
  237.                 ContaLetrasDif();
  238.             break;
  239.             case '6':
  240.                 Palindromo();
  241.             break;
  242.             case '7':
  243.                 TiraEspacos();
  244.             break;
  245.             case '8':
  246.                 Duplica();
  247.             break;
  248.             default:
  249.                 printf("\nOpcao Invalida\n");
  250.         }
  251.        
  252.         Op = Menu();
  253.     }
  254.    
  255.     getch();
  256. }
  257.  
  258. int main(void)
  259. {
  260.     Executa();
  261.    
  262.     return 1;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement