ENCODING program In C Programming Language

In this C Programming tutorial, We will learn how to Encoding in C Programming Language. We are going to implement a C program based on an interesting table with some rules.

I  will advise you to first try to solve it with yourself and if you need any help then check out the code mention at the end of the post.

 

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.

Example:

  1. 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.
  2. The G has a numerical value of 7. It encodes to a 5 and becomes an E.
  3. 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.

ENCODING program In C Programming Language

#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;

}


Leave a Reply