Phone number to word conversion program
I don't usually do this bc I can usually learn code from the internet but I am having trouble with this problem and I can't seem to find any help. Here it goes:
Write a C program that, given a seven-digit number, writes to a file every possible seven-letter word (does not have to be a real word, no dictionaries required) corresponding to that number, i.e. 2=abc 3=def, and so on. There are 2187 such words for each phone numer. Avoid using digits 1 and 0. Note: letters in the output file are uppercase. Do not accept phone numbers with a zero or one. When the user runs the program, prompt them for a phone number. If it is invalid, exit with an appropriate comment.
Thank you everyone!
Trevor
phone number to word conversion
i have a couple codes that might work for the phone number to word conversion but i don't know how I would export these into a text file.
Code:
#include <stdio.h>
int main(void)
{
char buf[BUFSIZ], *p;
printf("Enter phone number: ");
fflush(stdout);
fgets(buf,sizeof buf,stdin);
for (p = buf;*p;p++)
{
/* print out each character here, *p */
switch (*p)
{
case 'a':
case 'b':
case 'c':
printf("2");
break;
case 'd':
case 'e':
case 'f':
printf("3");
break;
case 'g':
case 'h':
case 'i':
printf("4");
break;
case 'j':
case 'k':
case 'l':
printf("5");
break;
case 'm':
case 'n':
case 'o':
printf("6");
break;
case 'p':
case 'q':
case 'r':
case 's':
printf("7");
break;
case 't':
case 'u':
case 'v':
printf("8");
break;
case 'w':
case 'x':
case 'y':
case 'z':
printf("9");
break;
}
}
return 0;
}
or
Code:
#include <stdio.h>
int main(){
int ch;
int i,res;
char buff[BUFSIZ];
printf ("Enter a phone number: ");
fgets(buff,sizeof(buff),stdin);
for(i=0;buff[i]!='\0';i++)
{
ch=buff[i];
if ('a' <= ch && ch <= 'c')
res = 2;
if ('d' <= ch && ch <= 'f')
res = 3;
if ('g' <= ch && ch <= 'i')
res = 4;
if ('j' <= ch && ch <= 'l')
res = 5;
if ('m' <= ch && ch <= 'o')
res = 6;
if ('p' <= ch && ch <= 's')
res = 7;
if ('t' <= ch && ch <= 'v')
res = 8;
if ('w' <= ch && ch <= 'y')
res = 9;
printf ("%d ", res);
}
getchar();
return 0;
}
Phone number to string conversion using recursion
Code:
// PhoneNumberToString.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
static int COUNT = 0;
char Letters[][4] = {
{'0'},
{'1'},
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'}
};
void ConvertPhoneNumberToString(char phone[], int nArraySize, int nStart)
{
if(nStart < nArraySize - 1)
{
char chDigit = phone[nStart];
if(chDigit != '-')
{
int nDigit = chDigit - '0';
for(int iLetterCount=0; iLetterCount < 4; ++iLetterCount)
{
char chLetter = Letters[nDigit][iLetterCount];
if(chLetter != 0)
{
phone[nStart] = chLetter;
ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
phone[nStart] = chDigit;
}
}
}
else
{
ConvertPhoneNumberToString(phone, nArraySize, nStart + 1);
}
}
else
{
COUNT++;
printf("[%d]=> %s\n", COUNT, phone);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char phone[] = {'5','0','2','-', '8','7','6','-','2','3','4','5', '\0'};
ConvertPhoneNumberToString(phone, sizeof(phone)/sizeof(char), 8);
char ch;
scanf("%c", &ch);
return 0;
}
From
--Ashish Patel