C++ || Telephone Digit Program – How To Convert Letters To Numbers Using C++
The following is a program with functions which demonstrates how to implement a telephone digit program which converts the letters on a phone number keypad to digits.
The program allows to convert more than one letter at a time, include a hyphen, and allows both upper and lower case.
For example, the input text of “get loan”, the output would be:
438-5626
1. Telephone Digit Program
The example below demonstrates how to convert letters to numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
// ============================================================================ // Author: Kenneth Perkins // Date: Nov 1, 2020 // Taken From: http://programmingnotes.org/ // File: telephone.cpp // Description: Converts letters to digits in a telephone number // ============================================================================ #include <iostream> #include <string> #include <map> #include <cctype> #include <exception> #include <stdexcept> #include <algorithm> std::string convertNumber(std::string phoneNumber) { // Remove whitespace from string phoneNumber.erase(std::remove_if(phoneNumber.begin(), phoneNumber.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); }), phoneNumber.end()); // Check if string is valid if (phoneNumber.length() < 1 || phoneNumber.length() > 10) { throw std::invalid_argument {phoneNumber + " is not a valid phone number"}; } const std::string keypad = "22233344455566677778889999"; std::map<char, char> alphabet; // Build the phone number mapping alphabet for (unsigned index = 0; index < keypad.length(); ++index) { alphabet['a' + index] = alphabet['A' + index] = keypad[index]; } // Convert the letters to numbers std::string result = ""; for (const auto& ch : phoneNumber) { auto number = ch; if (!std::isdigit(number)) { auto search = alphabet.find(number); if (search == alphabet.end()) { // Invalid input continue; } number = search->second; } result += number; } // Add the hyphens if necessary if (result.length() > 9) { result.insert(result.length() - 4, "-"); } if (result.length() > 3) { result.insert(3, "-"); } return result; } int main() { std::cout << "This is a program to convert letters to" << " their corresponding telephone digits." << std::endl << std::endl << "Enter your letters: "; std::string phoneNumber = ""; std::getline(std::cin, phoneNumber); try { std::cout << "\nThe number converted: " << convertNumber(phoneNumber) << std::endl; } catch (std::exception& e) { std::cout << "\nAn error occurred: " << e.what(); } std::cin.get(); return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
(Note: the code was compiled separate times to display different output)
====== RUN 1 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: get loan
The number converted: 438-5626
====== RUN 2 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: getloan
The number converted: 438-5626
====== RUN 3 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: get-loan
The number converted: 438-5626
====== RUN 4 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: GETLOAN
The number converted: 438-5626
====== RUN 5 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: G e T L O a N
The number converted: 438-5626
====== RUN 6 ======
This is a program to convert letters to their corresponding telephone digits.
Enter your letters: 6572782011
The number converted: 657-278-2011
Leave a Reply