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:

#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();
}

You might also like:

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

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x