Question

In: Computer Science

C++. Write a program that asks the user to enter a single word and outputs the...

C++.

Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel.

See sample screen output for an example:

 
    Enter a word: program    
    Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike


The specific requirement is that you write the program so that it determines the word corresponding to a specified letter using a Switch statement instead of an If structure.
As a point of reference, the ICAO alphabet is included below:

 
        A       Alpha           N       November
        B       Bravo           O       Oscar
        C       Charlie         P       Papa
        D       Delta           Q       Quebec
        E       Echo            R       Romeo
        F       Foxtrot         S       Sierra
        G       Golf            T       Tango
        H       Hotel           U       Uniform
        I       India           V       Victor
        J       Juliet          W       Whiskey
        K       Kilo            X       X-Ray
        L       Lima            Y       Yankee
        M       Mike            Z       Zulu

HINT: You may consider using character (char) array related processing or if you prefer to work with strings, determine the length of the variable string word and iterate through each letter in the string word using a for loop with a nested switch statement that has the necessary case labels matching the ICAO alphabets. Suggest converting all letters to uppercase to match the ICAO alphabet format.

Be sure to use proper formatting and appropriate comments in your code. Provide appropriate prompts to the user. The output should be clearly labeled and neatly formatted.

Solutions

Expert Solution

Code Explanation:

  • Include the required libraries
  • declare n and i as int and initialize i to 0
  • Start the main function
  • In the main function declare a char array word
  • Input a word and assign to word
  • find the length of word using strlen() which is present in string.h library
  • Use while loop until i is not equal to n
  • Then inside for loop use switch case with (char)toupper(word[i]). Here we converted each character in word to upper because if we input lower chars also it matches in case with upper chars. Otherwise we need to write two cases for lower and upper chars.
  • print respective phonetic version using the cases.

Code:

#include <iostream>
#include<string.h>
using namespace std;

int main()
{
int n, i = 0;
char word[100];
cout << "Enter a word: ";
cin >> word;
n =strlen(word);
cout << "Phonetic version is: ";
while(i!=n)
{
switch((char)toupper(word[i]))
{

case 'A' : cout << "Alpha "; break;
case 'B' : cout << "Bravo "; break;
case 'C' : cout << "Charlie "; break;
case 'D' : cout << "Delta "; break;
case 'E' : cout << "Echo "; break;
case 'F' : cout << "Foxtrot "; break;
case 'G' : cout << "Golf "; break;
case 'H' : cout << "Hotel "; break;
case 'I' : cout << "India "; break;
case 'J' : cout << "Juliet "; break;
case 'K' : cout << "Kilo "; break;
case 'L' : cout << "Lima "; break;
case 'M' : cout << "Mike "; break;
case 'N' : cout << "November "; break;
case 'O' : cout << "Oscar "; break;
case 'P' : cout << "Papa "; break;
case 'Q' : cout << "Quebec "; break;
case 'R' : cout << "Romeo "; break;
case 'S' : cout << "Sierra "; break;
case 'T' : cout << "Tango "; break;
case 'U' : cout << "Uniform "; break;
case 'V' : cout << "Victor "; break;
case 'W' : cout << "Whiskey "; break;
case 'X' : cout << "X-Ray "; break;
case 'Y' : cout << "Yankee "; break;
case 'Z' : cout << "Zulu "; break;
default: cout << "default char";
}
i++;

}
cout << "\n";
return 0;
}

Sample O/P1:

Enter a word: program
Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike

Sample O/P2:

Enter a word: Apple
Phonetic version is: Alpha Papa Papa Lima Echo

Sample O/P3:

Enter a word: NewYork
Phonetic version is: November Echo Whiskey Yankee Oscar Romeo Kilo

Code Screenshot:

Sample O/P1 screenshot:

Sample O/P2 screenshot:

Sample O/P3 screenshot:


Related Solutions

Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0. using for-loop
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0.
Write a Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT