arrays - Needs Logic explanation java reverse string -
this question has answer here:
- how can char array in reverse order? 5 answers
question of reverse string without using string function didn't inner loop why s.length()-1
? why -1 ? have multi dimensional arrays?
char ch[]=new char[s.length()]; for(i=0;i < s.length();i++) ch[i]=s.charat(i); for(i=s.length()-1;i>=0;i--) system.out.print(ch[i]);
found code
the indices of java string's characters go 0 string's length - 1 (just indices of java array start in 0, indices of string).
therefore in order print string in reverse order, second loop iterates s.length()-1 0. prior that, first loop iterates 0 s.length()-1 in order copy characters of string character array.
this has nothing multi-dimensional arrays.
Comments
Post a Comment