Encoding

QUESTION:

In this program you will be given a letter to encode. The difference here is that different rules are used for different letters and the counting process can cause you to wrap around the alphabet. Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:

If the letter is between the given letters, inclusive:
The number of letters to count is given by:
A–E
Multiply its numerical value by 2
F–J
Divide its numerical value by 3. Multiply the integer remainder by 5
K–O
Divide its numerical value by 4. Multiply the integer remainder by 8.
P–T
Add 10.
U- Z
Find the largest integer factor of its numerical value less than the value itself. Multiply it by 12.

As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a  4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times and ending at Z. If a numerical value of zero is evaluated print a # symbol.
[Hint: ASCII value of A is 65].
INPUT:
Input consists of an upper case letter.
OUTPUT:
Print the encoded letter it produces.
SAMPLE INPUT 1
B
SAMPLE OUTPUT 1
D
SAMPLE INPUT 2
Z
SAMPLE OUTPUT 2
Z

SOLUTION:

#include<stdio.h>
int main()
{
    unsigned char c,r;
    int k,r1=0,i=2;
    scanf("%c",&c);
    c=c-64;
    if(c>=1 && c<=5)
        k=1;
    else if(c>=6 && c<=10)
        k=2;
    else if(c>=11 && c<=15)
        k=3;
    else if(c>=16 && c<=20)
        k=4;
    else if(c>=21 && c<=26)
        k=5;
    switch(k)
    {
        case 1:
            r=c*2;
            break;
        case 2:
            r=(c%3)*5;
            break;
        case 3:
            r=(c%4)*8;
            break;
        case 4:
            r=c+10;
            break;
        case 5:
        {
                for(i=2;i<c;i++)
                {
                    r1=c%i;
                    if(r1==0)
                        r=i*12;
                }
                break;
        }
    }
    if(r==0)
        r=-29;
    else if(r>26)
    {
        r=r%26;
        if(r==0)
            r=26;
    }
    r=r+64;
    printf("%c",r);
    return 0;
}

Comments

Popular posts from this blog

Implementing a Fixed size Queue of maximum size 5 using an array

Implementing a Dynamically sized stack using array

Functions - Null Matrix