java - Converting chars into a string (without array)? -
i need convert set of chars string. problem is, don't know how without array (it's forbidden utilize them, because didn't see subject yet).
so method starts asking user type in word (string type). in case utilize word "programma" example. first step count +4 every character of word. in illustration must alter "programma" --> "tvskveqqe".
i split input string "programma", seperate chars, , added +4 in alfabet. afterwards made sure if letters "wxyz" used, converted w --> a, x -->b, y --> c , z --> d.
but i'm stuck @ part, need set chars 't''v''s''k''v''e''q''q''e' string "tvskveqqe", , utilize homecoming statement.
thanks!
public char coderen() { string str; //input string char c, e = ' '; int = 4, b, d; system.out.println("geef een woord in: "); str = input.readstring(); //input (int = 0; < str.length(); i++) { // splits string separate chars b = (int) str.charat(i) + a; // +4 in ascii c = (char) b; if (c >= 'e' && c <= 'z') { e = c; system.out.println(e); } else if (c >= '{' && c <= '~') { // converts 'w''x''y''z' 'a''b''c''d' d = (int)c - 26; e = (char) d; system.out.println(e); } else { system.out.println("fout!"); } } homecoming e; }
it's bit unusual should not utilize arrays in end string represented array of characters, argue approach apply "using array"...
having said that, seek different things:
using stringbuilder:
stringbuilder sb = new stringbuilder(); .... { sb.appendchar(c); }
using string concatenation:
string result = ""; .... { result = result + c; }
using string replace: (however not based on index on matching characters):
for (char c = 'a'; c <= 'z'; c++) { result = result.replace(c, c + 4); }
java string char converter
No comments:
Post a Comment