Decode the given digit sequence into the characters

Decode the given digit sequence into the characters

Question:

Decode the given digit sequence and print the characters for that decodings.

Logic:

  • Get the input from the user.
  • Then find the possible decodings and combinations in decodings are <27.
  • Print corresponding characters for the decodings.

Algorithm:

  1. Get the input as a string from the user.
  2. Then convert the string into corresponding integers and stores to an integer array.
  3. The current and next elements are joined and compared for <27.
  4. If the condition satisfies swap the elements otherwise check for the next one.
  5. Then redivide the elements into single digits.
  6. For combinations print the corresponding characters.

Program:

[code lang=”c”]
#include<stdio.h>
void character(int a[],int m)//prints corresponding characters for intergers
{
int i;
for(i=0;i<m;i++)
{
printf("%d,",a[i]);
}printf("=>");
for(i=0;i<m;i++)
{
printf("%c",a[i]+64);//converts the integer to character using ASCII
}
printf("\n");
}
void main()
{
int i,m=0,n,k,j,b[20];
char a[20];
clrscr();
printf("enter the number:");
gets(a);
n=strlen(a);
m=n;
for(i=0;i<n;i++)//copies the character array elements to integer array by converting elements to integers
{
b[i]=a[i]-48;
}
character(b,n);//prints characters for individual digits
for(i=0;i<n;i++)
{
for(j=i;j<m-1;j++)
{
if(((b[j]*10)+b[j+1])<27)//current and next elements are joined and compared
{
m–; //decrements the length of array
for(k=j;k<m;k++) // swaps the elements in array
{
if(k==j)
{
b[j]=(b[j]*10)+b[j+1];
}
else
{
b[k]=b[k+1];
}
}
character(b,m);//prints the characters
}
else//skips the repeated combinations
{
j=j+2;
}
}
for(j=0;j<m;j++) //redivide the elements into a single digit
{
if(b[j]>9)
{
m++;
for(k=m-1;k>j;k–)
{
b[k]=b[k-1];
}
b[j+1]=b[j]%10;
b[j]=b[j]/10;
}
}
}
getch();
}
[/code]

You might also like:

Sum of digits of a number until the sum is a single digit

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *