DrGustav

frequencia

Jul 13th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. void ordena_bolha(char v[], int n)
  6. {
  7.     int i,j;
  8.     for (i = 0; i < n; i++)
  9.     {
  10.         // encontra o menor elemento
  11.         for (j = 1; j < n-i; j++)
  12.             if (v[j-1] > v[j])
  13.             {
  14.                 // faz a troca de posicoes entre o menor e a posicao i. (copiando estruturas).
  15.                 troca(&v[j-1],&v[j]);
  16.                 // ou pode fazer sem chamar a funcao.
  17.                 //temp = v[j-1];
  18.                 //v[j-1] = v[j];
  19.                 //v[j] = temp;
  20.             }
  21.     }
  22.  
  23. }
  24. void troca(char *a,char *b)
  25. {
  26.     char p = *a;
  27.     *a    = *b;
  28.     *b    = p;
  29. }
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35.     char frase[80],frase2[80];
  36.     int cont,i,j,soma=0;
  37.     char *p,*c;
  38.     gets(frase);
  39.     cont = strlen (frase);
  40.     strcpy(frase2,frase);
  41.     ordena_bolha(frase,cont);
  42.     for(i=0;i<cont;i++)
  43.     {
  44.         soma = 0;
  45.         p = &frase[i];
  46.         for(j=0;j<cont;j++)
  47.         {
  48.             c = &frase[j];
  49.             if(*p == *c)
  50.             {
  51.                 soma++;
  52.                 if((soma > 1) && (i != j) && (*p == *c))
  53.                     *c = '0';
  54.             }
  55.         }
  56.     if(frase[i] != '0' && frase[i] != ' ')
  57.     printf("%c %d\n",frase[i],soma);
  58.     }
  59. return 0;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment