Salve a tutti, questo codice dovrebbe generare la parte di codice fiscale relativa al cognome di una persona. Vorrei capire gentilmente cosa fa il blocco denominato "bool is_in(const char element, const char* set)". Grazie
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "cf-surname.h"
#define SURNAME_CODE_LEN 3
#define FILL_CHAR 'X'
static const char* CONSONANTS = "QWRTYPSDFGHJKLZXCVBNM";
static const char* VOWELS = "EUIOA";
bool is_in(const char element, const char* set)
{
bool found = false;
int i = 0;
while (set[i] != '\0' && !found) {
found = (set[i]==element);
i++;
}
return found;
}
char* get_surname_code(const char* surname, char* code){
assert(surname != NULL);
int idx_code = 0; // indice su code
int idx_surname = 0; // indice su surname
bool stop = false; // true sse la scansione su surname è terminata
// Passo 1: trovo le consonanti
while (idx_code < SURNAME_CODE_LEN && !stop){
bool found = false;
while (idx_surname < strlen(surname) && !found){
if (is_in(toupper(surname[idx_surname]),CONSONANTS)) {
found = true;
} else {
idx_surname++;
}
}
// esco se ho trovato una consonante oppure ho terminato
if (found) {
code[idx_code] = toupper(surname[idx_surname]);
idx_code++;
idx_surname++;
} else {
stop = true;
}
}
// Passo 2: trovo le vocali
idx_surname = 0;
stop = false;
while (idx_code < SURNAME_CODE_LEN && !stop){
bool found = false;
while (idx_surname < strlen(surname) && !found){
if (is_in(toupper(surname[idx_surname]),VOWELS)) {
found = true;
} else {
idx_surname++;
}
}
// esco se ho trovato una vocale oppure ho terminato
if (found) {
code[idx_code] = toupper(surname[idx_surname]);
idx_code++;
idx_surname++;
} else {
stop = true;
}
}
// Passo 3: Riempio code con carattere di riempimento
while (idx_code < SURNAME_CODE_LEN) {
code[idx_code] = FILL_CHAR;
idx_code++;
}
code[idx_code] = '\0'; // code è una stringa
// verifica post-condizioni
assert(strlen(code) == SURNAME_CODE_LEN);
for (int i=0; i<SURNAME_CODE_LEN; i++)
assert(is_in(code[i], CONSONANTS) || is_in(code[i], VOWELS));
return code;
}