/****************************************************************************
*
* t9.c -- Codifica T9
*
* Copyright (C) 2021, 2022 Moreno Marzolla
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
****************************************************************************/
/***
% LabASD - Codifica T9
% Moreno Marzolla
% Ultimo aggiornamento: 2022-02-19
![](t9.png)
Realizzare un programma che stampa tutte le parole presenti nel file
[wordlist.txt](wordlist.txt) che hanno la stessa codifica T9 di una
parola specificata sulla riga di comando. La codifica T9 associa a
ciascuna delle 26 lettere dell'alfabeto un numero, come indicato
nella Tabella 1.
: Tabella 1: codifica T9
Ognuno dei caratteri corrisponde a
---------------------- ---------------
`abc` `2`
`def` `3`
`ghi` `4`
`jkl` `5`
`mno` `6`
`pqrs` `7`
`tuv` `8`
`wxyz` `9`
---------------------- --------------
Quindi le lettere `a`, `b` e `c` vengono tutte codificate con il
singolo carattere `2`, le lettere `d`, `e`, `f` dal carattere `3` e
così via; non si fa distinzione tra lettere maiuscole e
minuscole. Questo tipo di codifica era utilizzato dai vecchi
cellulari.
Alcuni esempi:
La stringa diventa
-------------- -------------
`Informatica` `46367628422`
`algoritmi` `254674864`
`ciao` `2426`
-------------- -------------
Si può assumere che le stringhe in input abbiano lunghezza
strettamente minore di `MAXLEN`, e che contengano esclusivamente
lettere maiuscole o minuscole; non si deve fare distinzione fra
maiuscole e minuscole. Ricordiamo che per convertire un carattere $c$
in minuscolo si può usare la funzione `int tolower(int c)` dichiarata
in ``.
Alcuni esempi di esecuzione del programma:
+---------------------+-----------------------+
| Input | Output |
+=====================+=======================+
| `./t9 ciao` | ``` |
| | ciao 2426 |
| | cibo 2426 |
| | 2 occorrenze |
| | ``` |
+---------------------+-----------------------+
| `./t9 tavolo` | ``` |
| | tavolo 828656 |
| | 1 occorrenza |
| | ``` |
+---------------------+-----------------------+
| `./t9 etna` | ``` |
| | duna 3862 |
| | etna 3862 |
| | fuma 3862 |
| | 3 occorrenze |
| | ``` |
+---------------------+-----------------------+
| `./t9 tablet` | ``` |
| | 0 occorrenze |
| | ``` |
+---------------------+-----------------------+
Si noti che la parola specificata sulla riga di comando non deve
necessariamente comparire nel file [wordlist.txt](wordlist.txt).
## File
- [t9.c](t9.c)
- [wordlist.txt](wordlist.txt)
***/
#include
#include
#include
#include
#include
/* Data una stringa s, composta solo da lettere, scrive nella stringa
t la corrispondente codifica T9. t deve puntare ad un blocco di
memoria già allocato dal chiamante con spazio sufficiente. */
void t9(const char* s, char *t)
{
/* Questa soluzione sfrutta l'idea di costruire una tabella di
conversione lettera->numero. La tabella è l'array t9enc[].
t9enc[0] è la codifica T9 della lettera 'a', t9enc[1] è la
codifica T9 della lettera 'b', ... t9enc[25] è la codifica T9
della lettera 'z'. Per ottenere la codifica di una lettera il
cui codice ASCII è memorizzato in una variabile c, scriveremo
t9enc[c - 'a'] (infatti il codice ASCII di 'a' non è zero,
quindi t9enc['a'] causerebbe un accesso out-of-bound nell'array
t9enc) */
static const char* t9enc = "22233344455566677778889999";
int i;
for (i=0; s[i]; i++ ) {
const char c = tolower(s[i]);
assert( isalpha(c) );
t[i] = t9enc[ c - 'a' ];
}
t[i] = '\0';
}
#define MAXLEN 100
int main( int argc, char *argv[] )
{
int nocc = 0;
char inputw_t9[MAXLEN], filew[MAXLEN], filew_t9[MAXLEN];
FILE *f;
if (argc != 2) {
fprintf(stderr, "Uso: %s parola\n", argv[0]);
return EXIT_FAILURE;
}
if ((f = fopen("wordlist.txt", "r")) == NULL) {
fprintf(stderr, "Errore nell'apertura di \"wordlist.txt\"\n");
return EXIT_FAILURE;
}
t9(argv[1], inputw_t9);
while (1 == fscanf(f, "%s", filew)) {
t9(filew, filew_t9);
if (0 == strcmp(filew_t9, inputw_t9)) {
printf("%s %s\n", filew, filew_t9);
nocc++;
}
}
printf("%d occorrenz%c\n", nocc, nocc != 1 ? 'e' : 'a');
fclose(f);
return EXIT_SUCCESS;
}