UVA Problem 10921 – Find the Telephone Solution

UVA Problem 10921 – Find the Telephone Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Given a string of alphabets (A-Z), 1, 0, and Hyphens( – ) task for this problem is to find numbers represented by a character from the given table.

1, 0, Hyphen should not be changed since they aren’t in table. Just change a character with its integer representation from table. Easiest way to do this is create a mapping array with pattern from the table. Then map the pattern to each character and print result.

 

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer. Please compile with c++ compiler as some of my codes are in c and some in c++.


More Inputs of This Problem on uDebug.


Input:

1-HOME-SWEET-HOME
MY-MISERABLE-JOB

 


Output:

1-4663-79338-4663
69-647372253-562

Code:

/**
 * Author:    Asif Ahmed
 * Site:      https://quickgrid.wordpress.com
 * Problem:   UVA 10921 - Find The Telephone
 * Technique: Mapping character from an array to another.
 */

#include<stdio.h>
#include<string.h>


#define N 128


static char mappingArray[N] = "22233344455566677778889999";


static char table[N];
static char input[N];


int main(){

    /**
     * The code below fill 'A' to 'O' with appropriate values.
     * Not necessary when using mapping array.
     */
    /*
    for(int i = 0; i <= 15; ++i ){
        table[64 + i] = k;
        if(i % 3 == 0) ++k;
    }
    */


    int k = 0;

    /**
     * Map each character index to integer value from mapping array.
     */
    for(int i = 'A'; i <= 'Z'; ++i)
        table[i] = mappingArray[k++];


    /**
     * These characters don't change.
     */
    table['-'] = '-';
    table['0'] = '0';
    table['1'] = '1';


    /**
     * Take input, then iterate through each character
     * and print the character indexed value from table.
     */
    while( gets(input) ){
        for(int i = 0; input[i]; ++i)
            printf("%c", table[ input[i] ]);
        printf("\n");
    }

    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s