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:
- Get the input as a string from the user.
- Then convert the string into corresponding integers and stores to an integer array.
- The current and next elements are joined and compared for <27.
- If the condition satisfies swap the elements otherwise check for the next one.
- Then redivide the elements into single digits.
- 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(); }