Decoder


General Statement: Decode a secret message that has been encoded as follows:
1) all letters are upper case
2) a space is indicated by an underscore (_)
3) a number is preceded by the number symbol (#)
4) all other non-letter characters are unchanged
5) each letter was converted to a number that is the sum of its base number and its position in the word
6) for the base number, A=1, B=2, C=3, etc.

Input: The first line of the data set for this problem is an integer that represents the number of messages that follow. Each message is on a separate line.

Output: All letters are upper case. The only position a space is to be output is where the underscore symbol (_) appears in the encoded message. The output is to be formatted exactly like that for the sample output given below.

Assumptions: There is 1 space between each item on the data line.

Sample Input:
3
14 21 13 2 5 18 _ # 2 _ 16 5 14 3 9 12
1 16 18 9 12 _ # 28 , _ # 2001
16 18 15 2 12 5 13 _ # 9 . # 1

Sample Output:
NUMBER 2 PENCIL
APRIL 28, 2001
PROBLEM 9.1

Solutions :


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

void number_conversion (char number [])
{
    int alphabet = atoi (number);

    alphabet += 64;

    printf ("%c", alphabet);
}

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    char str [500];

    while ( dataset-- ) {
        gets (str);

        char number [3];
        int length = strlen (str);
        int i = 0;

        while ( i < length ) {

            if ( str [i] == '#' ) {
                i += 2;
                while ( str [i] != ' ' )
                    printf ("%c", str [i++]);
            }

            else if ( str [i] == '_' )
                printf (" ");

            else if ( str [i] >= '1' && str [i] <= '9' ) {
                number [0] = str [i++];
                if ( str [i] >= '0' && str [i] <= '9' ) {
                    number [1] = str [i++];
                    number [2] = 0;
                }
                else
                    number [1] = 0;

                number_conversion (number);
            }

            else if ( str [i] != ' ' )
                printf ("%c", str [i]);
            i++;
        }

        printf ("\n");
    }

    return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s