I am pretty new to C and I am working on an assignment for a class. This is only the first function of it but the part of the program I'm working on now is supposed to take a phone number and convert the last 7 digits to letters (like a flip phone). We would need to check which are actual words from the combinations of the letters, given a dictionary text doc, by sending the output to another function.
This first function just converts it to letters and spits out possible combinations. The problem I am having with it is that it prints (in the function) as it should but the return output is weird ascii.
const char *generate(char chararr[]) {
static char *dial[] = { "", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ", '\0'}; //pointers to strings
char *three, *four, *five, *six, *seven, *eight, *nine; //pointers to chars
char number[12] = {chararr[0], chararr[1], chararr[2], chararr[4], //convertes pho-one-number to phonenumber
chararr[5], chararr[6], chararr[8], chararr[9], chararr[10], chararr[11], '\0'};
const char *output1;//stores output for return value
for (three = dial[ number[ 3 ] - '0' ]; *three; three++ ) {
for (four = dial[ number[ 4 ] - '0' ]; *four; four++ ) {
for (five = dial[ number[ 5 ] - '0' ]; *five; five++ ) {
for (six = dial[ number[ 6 ] - '0' ]; *six; six++ ) {
for (seven = dial[ number[ 7 ] - '0' ]; *seven; seven++ ) {
for (eight = dial[ number[ 8 ] - '0' ]; *eight; eight++ ) {
for (nine = dial[ number[ 9 ] - '0' ]; *nine; nine++ ) {
char word[8]; //stores the word
word[0] = *three;
word[1] = *four;
word[2] = *five;
word[3] = *six;
word[4] = *seven;
word[5] = *eight;
word[6] = *nine;
word[7] = '\0';
output1 = word; //stores word in output1
printf("%s\n", output1); //<--------------printing the output
return output1; //<----------------- returning the output
}
}
}
}
}
}
}
}
Also for reference here is main:
int main() {
char num[13];
printf("Enter a phone number: "); //enter and store number
scanf("%s", &num);
if (sizeof(num) < 13 || sizeof(num) > 13 || (num[3] != '-' || num[7] != '-')) {
printf("Invalid Format\n");
exit;
}
else {
// generate(num);
printf("%s\n", generate(num));
}
//printf("%s\n", num);
//generate(chararr);
return 0;
}
Actual results:
Enter a phone number: 234-456-2344
GJMADGG
□□□
Expected results:
Enter a phone number: 234-456-2344
GJMADGG
GJMADGG