Home > database >  C Convert Number to Text With Text
C Convert Number to Text With Text

Time:10-29

i want to numbers in the text entered by the user are converted into text and printed on the screen. Example:

cin>> My School Number is 5674 and i want to "my school number is five six seven four" output like this. I make only Convert to number to text but i cant put together text and numbers please help me

`


#include <iostream>
using namespace std;
void NumbertoCharacter(int n)
{
    int rev = 0, r = 0;

    while (n > 0) {

        r = n % 10;
        rev = rev * 10   r;
        n = n / 10;
    }

    while (rev > 0) {
        r = rev % 10;

        switch (r) {
        case 1:
            cout << "one ";
            break;
        case 2:
            cout << "two ";
            break;
        case 3:
            cout << "three ";
            break;
        case 4:
            cout << "four ";
            break;
        case 5:
            cout << "five ";
            break;
        case 6:
            cout << "six ";
            break;
        case 7:
            cout << "seven ";
            break;
        case 8:
            cout << "eight ";
            break;
        case 9:
            cout << "nine ";
            break;
        case 0:
            cout << "zero ";
            break;
        default:
            cout << "invalid ";
            break;
        }
        rev = rev / 10;
    }
}

int main()
{
    int n;
    cin >> n;
    NumbertoCharacter(n);
    return 0;
}

`

CodePudding user response:

It would be much easier if you built the output into a string, instead of using individual cout statements. Then the NumberToCharacter function just needs to build the string and return the results.

The second issue is one of redundancy: You have 10 separate if statements for each digit, when it would be much simpler to have an array that has a string that corresponds to the digit found.

Putting that all together, here is a proposed solution:

#include <iostream>
#include <string>

std::string NumbertoCharacter(int n)
{
    std::string word[] = {"zero", "one", "two", "three", 
                          "four", "five", "six", "seven", 
                          "eight", "nine"};
    std::string output;

    int r;
    while (n > 0) 
    {
       r = n % 10;
       output = word[r]   " "   output;
       n = n / 10;
    }
    return output;
 }

int main()
{
    std::cout << "My School Number is " << NumbertoCharacter(5764) << "\n";
    std::cout << "My School Number is " << NumbertoCharacter(5) << "\n";
    std::cout << "My School Number is " << NumbertoCharacter(50) << "\n";
    return 0;
}

Output:

My School Number is five seven six four 
My School Number is five 
My School Number is five zero 

CodePudding user response:

Here is a solution. The key line is the int charToInt = a - '0';. Here is a link to more details on that technique. If the value returned (charToInt) is between 0 and 9, then the character is a valid integer and can be converted. If not, then just print the original character.

I also changed cin to getline (documentation here) so that your input can accept multiple words.

void NumbertoCharacter(string n)
{
    for (auto a : n) {
        int charToInt = a - '0';
        if (charToInt >= 0 && charToInt <= 9) {
            switch (charToInt) {
                case 1:
                    cout << "one ";
                    break;
                case 2:
                    cout << "two ";
                    break;
                case 3:
                    cout << "three ";
                    break;
                case 4:
                    cout << "four ";
                    break;
                case 5:
                    cout << "five ";
                    break;
                case 6:
                    cout << "six ";
                    break;
                case 7:
                    cout << "seven ";
                    break;
                case 8:
                    cout << "eight ";
                    break;
                case 9:
                    cout << "nine ";
                    break;
                case 0:
                    cout << "zero ";
                    break;
                default:
                    cout << "invalid ";
                    break;
            }
        }
        else {
            cout << a;
        }
    }
    cout << endl;
}

int main(int argc, char** argv) {
    string n;
    getline(cin, n);
    NumbertoCharacter(n);
    return 0;
}

CodePudding user response:

I'll give you high-level plan, no doubts you can implement it:

  • Read input to string
  • Iterate through this string:
    • print leading non-numbers as is
    • print numbers converted as you want Done!
  • Related