arrays - Encode printing of alphabets in C -
in program have encoded using numerical value of each letter (a=1, b=2, ... z= 26) rules follows: a–e multiply numerical value 2 f–j divide numerical value 3. multiply integer remainder 5 k–o divide numerical value 4. multiply integer remainder 8. p–t add 10. u- z find largest integer factor of numerical value less value itself. multiply 12.
as example if letter encode b, b has numerical value of 2 , encodes 4 , becomes d, 4th letter of alphabet.
the g has numerical value of 7. encodes 5 , becomes e.
the numerical value of z 26. largest factor 13. must count 156 (13*12) letters. has effect of wrapping around alphabet 6 complete times , ending @ z. if numerical value of 0 evaluated print # symbol. problem code - not able print #. - whenever entering y getting j why???????
here code:
#include<stdio.h> void main() { char c; int n,i,max=0,j,y; int a[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90}; scanf("%c",&c); n=c; for(i=0;i<5;i++) { int flag=0; if(a[i]==n ) { flag=1; } if(flag==1) { printf("%c",a[((i+1)*2)-1]); break; } } for(i=5;i<10;i++) { int flag=0; if(a[i]==n ) { flag=1; } if(flag==1) { printf("%c",a[(((i+1)%3)*5)-1]); break; } } for(i=10;i<15;i++) { int flag=0; if(a[i]==n ) { flag=1; } if(flag==1) { printf("%c",a[(((i+1)%4)*8)-1]); break; } } for(i=15;i<20;i++) { int flag=0; if(a[i]==n ) { flag=1; } if(flag==1) { printf("%c",a[((i+1)+10)-1]); break; } } for(i=15;i<26;i++) { int flag=0; if(a[i]==n ) { flag=1; for(j=1;j<=(i+1)/2;j++) { if((i+1)%j==0) { if(max<j) { max=j; } } } } if(flag==1) { y=(max*12)/6; if(y>=0) { printf("%c",a[y-1]); } else { printf("#"); } break; } } } help me out guy's.
you have variety of rules encoding characters, @ end of day, each letter of alphabet maps single output character. can create table contains outputs correspond each letter. code reduces following
int main( void ) { char table[] = "bdfhj#ej#ex#hpxzabcdfbljhz"; char c; if ( scanf("%c",&c) == 1 && c >= 'a' && c <= 'z' ) printf( "%c\n", table[c - 'a'] ); } note table in code attempts follow rules specified. rules don’t create 1-to-1 mapping of input output, of course it’s trivial change table achieve mapping like.
Comments
Post a Comment