java - Why do I get an extra output in my code? -
i'm trying write code gets input array of strings, , creates two-dimensoinal array of type char, number of lines number of words in string , in each line there reversed array of letters in word.
for example if input is: start @ end!
my output :
[t, r, a, t, s]
[t, a]
[e, h, t]
[!, d, n, e]
this code:
public static void main(string[] args) { char thelist[][] = new char[args.length][]; (int = 0; < args.length; i++) { thelist[i] = new char[args[i].length()]; (int k = 0; k < args[i].length(); k++) { char letter = args[i].charat(args[i].length() - k - 1); thelist[i][k] = letter; } (char[] word : thelist) { string list = arrays.tostring(word); system.out.println(list); } } }
the code should :
string str ="start @ end!"; string[] splitstring = str.split(" "); int length=0; for(string string:splitstring ){ if(string.length()>length){ length=string.length(); } } character[][] chararray = new character[splitstring.length][length]; int index=0; for(string string:splitstring ){ length=string.length(); for(int i=0;i<string.length();i++){ chararray[index][--length]=string.charat(i); } index++; } (int i=0;i<chararray.length;i++){ (int j=0;j<chararray[i].length && chararray[i][j]!=null;j++) { system.out.print(chararray[i][j]); } system.out.println(); }
output:
trats ta eht !dne
Comments
Post a Comment