Advertisement
Guest User

final

a guest
Feb 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <assert.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int is_l(char c){ //Se è una lettera ritorna il valore 1 altrimenti 0
  7.     if (c <='z' && c >= 'a') return 1;
  8.     if (c <='Z' && c >= 'A') return 1;
  9.     return 0;
  10. }
  11. int is_c(char c){ //Se è una consonante ritorna 1 -- se è una vocale ritorna 0 -- nessuna dei due ritorna 2
  12.     if(is_l(c)){
  13.         char* check = "aeiouAEIOU";
  14.         for(;*check;check++){
  15.             if(c == *check) return 0;
  16.         }
  17.         return 1;
  18.     }
  19.     return 2;
  20. }
  21. int count_sil(char* stringa){ //Conta le sillabe (Conta i vocali)
  22.     int i = 0;
  23.     do{
  24.         if(is_c(*stringa)==0) i++;
  25.         stringa++;
  26.     }while(*stringa);
  27.     return i;
  28. }
  29. char* sillaba(char* stringa){
  30.     int n = count_sil(stringa);
  31.     char* finale;
  32.     char* mem = (char*)calloc(n+1,sizeof(char));
  33.     finale = mem;
  34.     for(;*stringa;stringa++,mem++){
  35.         if(is_c(*stringa)== 2){
  36.             *mem = '-';
  37.         }else{*mem = *stringa;}
  38.         if(is_c(*stringa)==0 && is_c(*(stringa+1))!=2 ){
  39.             if(is_c(*(stringa+1))==1){
  40.                 if(*(stringa+1)==*(stringa+2)){
  41.                     stringa++;
  42.                     mem++;
  43.                     *mem = *stringa;
  44.                 }
  45.             }
  46.             mem++;
  47.             *mem = '-';
  48.         }
  49.     }
  50.     *mem = '\0';
  51.     return finale;
  52. }
  53. int frequenza(char* s1, char*s2){
  54.     char* mem;
  55.     mem = s1;
  56.     int i = 0;
  57.     for(;*s2;){
  58.             if(*s1 == *s2){
  59.                 s1++;
  60.                 s2++;
  61.             }else s2++;
  62.             if (*s1 == '-' || *s1 == '\0'){
  63.                 i++;
  64.                 s1 = mem;
  65.             }
  66.             if (*s2 == '-'){
  67.                 s2++;
  68.             }
  69.     }
  70.     return i;
  71. }
  72. char* salto(char* s, int i){
  73.     int m = 0;
  74.     if(m==i)return s;
  75.     for(;*s;s++){
  76.         if(*s=='-')m++;
  77.         if(m==i)return (s+1);
  78.     }
  79.     return s;
  80. }
  81. char* copia(char* s){
  82.     char* mem = (char*)malloc(sizeof(char)*4);
  83.     int i;
  84.     for(i=0;*s!='-';s++,i++){
  85.         if(*s=='\0'){
  86.                 mem[i]='\0';
  87.                 return mem;
  88.         }
  89.         mem[i]= *s;
  90.     }
  91.     mem[i]='\0';
  92.     return mem;
  93. }
  94.  
  95. char* moda(char* s){
  96.     int sillabe = count_sil(s);
  97.     int i,n ;
  98.     int max = 0;
  99.     int risultato = 0;
  100.     char* camp = sillaba(s);
  101.     int freq[sillabe];
  102.     for(i=0;i!=sillabe;i++){
  103.         n = frequenza(salto(camp,i),camp);
  104.         freq[i] = n;
  105.     }
  106.     freq[i]='\0';
  107.     for(i=0;freq[i]!='\0';i++){
  108.         if(freq[i]>max){
  109.             max = freq[i];
  110.             risultato = i;
  111.         }
  112.     }
  113.  
  114.     char* fine;
  115.     fine = copia(salto(camp,risultato));
  116.  
  117.     return fine;
  118. }
  119.  
  120. int main(){
  121.     printf("%s\n", moda("mamma va va va va"));
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement