In this lab you format a string of 10 characters as a telephone number. Your pro
ID: 672485 • Letter: I
Question
In this lab you format a string of 10 characters as a telephone number.
Your program prompts the user to enter a telephone number as a string of 10 characters representing letters or numbers: {a – z, A - Z, 1 – 9} .
The characters in the input string can be a mix of digits, lower-case and upper-case characters.
Your program should convert UPPER-CASE letters to LOWER-CASE letters, and leave the digits unchanged.
Then your program should format your new string of 10 characters into a string of 13 characters that follow the standard format: (xxx)xxx-xxxx
Here is a sample run of the program:
The user enters FLOwer4YOU
Your program displays (flo)wer-4you
1) Writing the pseudo-code for this exercise will help you greatly.
The part of your program that processes the input string has less than 10 lines.
2) In order to convert selectively the letters in UPPER-CASE to LOWER-CASE, you can test the ASCII code of each characters in you string.
Here are the Java methods
a) that convert a character to its decimal ASCII value: int myASCII = (int)myChar;
b) that convert a decimal ASCII code to a character: char myChar = (char)myASCII;
You must use the ASCII codes for the conversion, or no credit for the assignment.
3) In Java a character cannot be “concatenated” to another character to make a string, but a character can be concatenated to a string (in this case, the compiler makes an automatic “cast” i.e., a type conversion), and the result is a string.
Provide pseudo-code of professional quality (sequence of instructions in plain English allowing the immediate translation into Java statements)
Use a loop for processing each character in the input string
Processing each character using its decimal ASCII-code
Display the output formatted as (xxx)xxx-xxxx
Follow requirements A and B
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
char startProgram;
char letters[256];
string length;
int maxLength;
bool run = true;
while(run=true)
{
cout <<" Enter Y or y to convert phone number from letters to numbers (enter any other letter to quit)"<<endl;
cin >> startProgram;
if(startProgram=='Y'|| startProgram=='y')
{
cout << " Enter the letters you would like your phone number to represent: "<<endl;
cin.get();
cin.getline(letters,300);
cout << " The corresponding number is: " << " "<<endl;
cout << endl;
length = letters;
maxLength = length.length();
if(maxLength > 8)
{
maxLength = 7;
}
for(int i=0; i < maxLength ; i++)
{
switch(letters[i])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
cout << 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
cout << 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
cout << 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
cout << 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
cout << 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
cout << 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
cout << 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
cout << 9;
break;
case ' ':
cout << " ";
default:
cout <<"Now quitting..."<<endl;
break;
}
if(i==2)
cout<<"-";}
}
else return 1;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.