java - Convert a letter to the corresponding letter on the opposite counting direction in the alphabet -
i self-studying java , @ beginning learning basics. below code trying convert letter corresponding letter on opposite counting direction in alphabet(i.e z or z etc.). works single letter not series of letters. how can make work more 1 letter? if can use simplest way quite new in java. don't(know how to) export built in classes etc.
thank you.
class turner{ int find(int fin, int mi,int ma,char ch[]){ int mid = (ma+mi)/2; int x; if(ch[mid]==fin) return mid; else if(fin<ch[mid]) return(find(fin, mi,mid-1,ch)); else return x = find(fin,(mid+1),ma,ch); } } class turn { public static void main(string args[]) throws java.io.ioexception { turner try1 = new turner(); char arra[] = new char[26]; char arrb[] = new char[26]; int min = 0; int max = arra.length; char = 'a'; char b = 'z'; int i; char letter; for(i=0;i<26;i++) { arra[i]=a; a++; arrb[i]=b; b--; } system.out.println("enter letter: "); letter = (char)system.in.read(); system.out.print(arrb[try1.find(letter,min,max,arra)]); } }
have considered doing math?
letter = character.touppercase((char)system.in.read()); system.out.print((char)('z' - (letter - 'a'));
and works 1 letter because not repeating conversion procedure. program reads 1 char
, prints opposite , terminates.
all have put read , print code inside sort of loop, every time runs promptly wait next letter.
while (true) { letter = character.touppercase((char)system.in.read()); if ((letter > 'z') || (letter < 'a')) break; // user inputted invalid symbol, terminating program system.out.print((char)('z' - (letter - 'a'))); }
Comments
Post a Comment