80 lines
1.9 KiB
C
80 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
//###############################
|
|
// ATTENTION CE CODE EST MOCHE #
|
|
//###############################
|
|
|
|
char most_frequent(const char *str){
|
|
|
|
//max
|
|
//temp max
|
|
//
|
|
//lettre
|
|
//temp lettre
|
|
//index
|
|
//index_clc
|
|
|
|
if (str == NULL) return '\0';
|
|
|
|
char *cha = malloc(2 * sizeof(char));
|
|
//{ lettre, temp}
|
|
if (cha == NULL) return '\0';
|
|
|
|
*cha = 0; // lettre
|
|
*(cha + 1) = 0; //lettre pendant le clc
|
|
|
|
int *values = malloc (4 * sizeof(int));
|
|
if(values == NULL) {
|
|
free(cha);
|
|
return '\0' ;
|
|
}
|
|
//{ max, temp max, index, index_clc}
|
|
|
|
*values = 0; // Maximum
|
|
*(values + 1) = 0; // Max temporaire
|
|
*(values + 2) = 0; // Index de la chaine
|
|
*(values + 3) = 0; // Index de calcul
|
|
|
|
//deplacement (index de la chaine)
|
|
while (*(str + *(values + 2)) != '\0'){
|
|
|
|
*(cha + 1) = *(str + *(values + 2)); //Traitement de la lettre à l'index x
|
|
*(values + 1) = 0; // RESET du max de clc
|
|
*(values + 3) = 0; // RESET de l'index de clc
|
|
|
|
//calcul pas du tt opti mais bon
|
|
while (*(str + *(values + 3)) != '\0'){
|
|
|
|
//si la lettre en traitment est la mm que celle rencontree
|
|
if (*(cha + 1) == *(str + *(values + 3))) {
|
|
(*(values + 1)) ++;
|
|
}
|
|
|
|
(*(values + 3)) ++;
|
|
}
|
|
|
|
if (*values < *(values + 1)) {
|
|
*values = *(values + 1); // temp -> max
|
|
*cha = *(cha + 1);
|
|
}
|
|
|
|
(*(values + 2)) ++;
|
|
}
|
|
char rslt = *cha;
|
|
free(values);
|
|
free(cha);
|
|
return rslt;
|
|
}
|
|
/*
|
|
int main () {
|
|
|
|
char test = most_frequent("WRYYYY!1!"); // 'Y'
|
|
printf("%c\n", test);
|
|
|
|
test = most_frequent("ORA ORA ORA!"); // 'O'
|
|
printf("%c\n",test);
|
|
test = most_frequent('\0'); // 'l'
|
|
printf("%c\n", test);
|
|
}*/
|