Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

can anyone explain how does this program work.

public static void main(String args[]){
    String input ="***NGuyen**Van******A*******";
    String result ="";
    for( int i = 0 ; i < input.length() ; i++)
    {

            result =  input.charAt(i) + result ;

        }


    System.out.println(result);

}}

The output will be

*******A******naV**neyuGN***
share|improve this question
    
what language ? –  PermGenError Jul 11 '13 at 21:23
    
Java language 11 –  user2565853 Jul 11 '13 at 21:26

2 Answers 2

It is actually a badly written program that constantly creates new (immutable) String objects while iterating over the characters of input from left to right, each time prepending the character to result. This is done by the code input.charAt(i) + result which puts the character in front of the previous result.

A better implementation would use a StringBuilder (which is mutable) to avoid excessive String creation.

Like this:

public static void main(String args[]) {
    String input = "***NGuyen**Van******A*******";
    StringBuilder builder = new StringBuilder();
    String result;
    for (int i = input.length() - 1; i >= 0; i--) {
        builder.append(input.charAt(i));
    }
    result = builder.toString();

    System.out.println(result);
}
share|improve this answer
    
I did it is that what you meant –  user2565853 Jul 11 '13 at 21:40
    
what did you do? –  herman Jul 11 '13 at 21:42
    
I did it is that what you meant " StringBuilder input = new StringBuilder("*NGuyenVan******A******* "); " it's my first time using it since I still beginner in programing . Thanks. –  user2565853 Jul 11 '13 at 21:42
    
No that is not what I meant, see the example I added to the answer. The result will be the same, only better in terms of performance and memory usage (although it is a simple program of course). –  herman Jul 11 '13 at 21:43
    
Thank you very mush. I will look for "builder" more now since it's important for memory . –  user2565853 Jul 11 '13 at 21:45

It is getting the characters from left to right (low to high, 0 to length)

It is appending the characters from right to left.

that reverses them.

1 2 3
| | |
\ | /
  X
/ | \
3 2 1

First time through the loop, it takes '1' and puts it in the string.

Second time through the loop, it takes '2' and puts it in the string to the left of '1'

Thirst time through the loop, it takes '3' and puts it to the left of '2' and '1'.

When I say "it takes" I'm referring to the code: input.charAt(i)

Note that the 'i' gets bigger each time through the loop because of the i++

share|improve this answer
    
Thank you , I got the idea now, but I want to know how does it work with the loop to change them from low to high left to right as what you explained. –  user2565853 Jul 11 '13 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.