Io l'ho fatto così. Però occhio ... è molto tosto!
codice:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define NUM_DIGITS 7
char *letters_arr[10] =
{
"", /* 0 */
"", /* 1 */
"abc", /* 2 */
"def", /* 3 */
"ghi", /* 4 */
"jkl", /* 5 */
"mno", /* 6 */
"pqrs", /* 7 */
"tuv", /* 8 */
"wxyz" /* 9 */
};
char *input_str (char *buf, int buflen)
{
char *p;
fgets (buf, buflen, stdin);
if ((p = strchr (buf, '\n')) != NULL)
*p = '\0';
return buf;
}
int is_str_digits (char *s)
{
while (*s != '\0')
{
if (!isdigit (*s))
return 0;
s++;
}
return 1;
}
int main (int argc, char *argv[])
{
char input[20];
int digits[NUM_DIGITS];
int count_letters[NUM_DIGITS];
int index_letters[NUM_DIGITS];
char combination[NUM_DIGITS+1];
int i, k, carry;
FILE *f;
do {
printf ("Inserire un numero di 7 cifre > ");
input_str (input, sizeof (input));
} while (strlen (input) != NUM_DIGITS || !is_str_digits (input));
if ((f = fopen ("parole.txt", "w")) != NULL)
{
for (i = 0; i < NUM_DIGITS; i++)
{
digits[i] = input[i] - '0';
count_letters[i] = strlen (letters_arr[digits[i]]);
index_letters[i] = 0;
}
do {
/*-- Genera stringa con la combinazione corrente --*/
for (i = 0, k = 0; i < NUM_DIGITS; i++)
{
if (count_letters[i] > 0)
combination[k++] = letters_arr[digits[i]][index_letters[i]];
}
combination[k] = '\0';
fprintf (f, "%s\n", combination);
/*-- Incrementa combinazione --*/
carry = 1;
for (i = 0; i < NUM_DIGITS; i++)
{
if (count_letters[i] > 0)
{
index_letters[i] += carry;
if (index_letters[i] >= count_letters[i])
{
index_letters[i] = 0;
carry = 1;
}
else
carry = 0;
}
}
} while (!carry);
fclose (f);
}
return 0;
}
EDIT: ho aggiunto la scrittura su file, non l'avevo messa subito.