1) Prompting the user to enter a phone number.
2) Read the characters, if its alphabets and lower case, change it to upper case. if not, leave it.
3) Read and store the characters in the first array O[i]
4) for statement for i
5) Switch-case loop. Here, I am trying to convert the entered alphabets into phone numbers and save them in a second array. However, if the input character was not an alphabet (none of the cases) i'm setting a default case to keep the same character but save it in the second array.
6) Print the 2nd array.
My outcome should be:
Enter Phone Number: 1-800-COL-LecT
Numeric Form: 1-800-265-5328
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
#define N 15
char O[N];
char S[N];
char ch;
int i=0;
printf("Enter phone number: ");
while ( (ch=toupper(getchar()) ) != '\n' && i<N)
{
O[i] = ch;
i++;
}
printf("Numeric Form : ");
for(i=0;i<N;i++)
{
switch (O[i])
{
case '.' :
case ',' :
case '@' :
S[i] = '1';
break;
case 'A' :
case 'B' :
case 'C' :
S[i] = '2';
break;
case 'D' :
case 'E' :
case 'F' :
S[i] = '3';
break;
case 'G' :
case 'H' :
case 'I' :
S[i] = '4';
break;
case 'J' :
case 'K' :
case 'L' :
S[i] = '5';
break;
case 'M' :
case 'N' :
case 'O' :
S[i] = '6';
break;
case 'P' :
case 'Q' :
case 'R' :
case 'S' :
S[i] = '7';
break;
case 'T' :
case 'U' :
case 'V' :
S[i] = '8';
break;
case 'W' :
case 'X' :
case 'Y' :
case 'Z' :
S[i] = '9';
break;
default:
O[i] = S[i];
printf("%c", S[i]);
}
i++;
}
return(0);
}