The following program reverses a string in a very straightforward, but rather inefficient way.
Tutorial Details:
The following program reverses a string in a very straightforward, but rather inefficient way. When you learn about StringBuilder (or the equivalent StringBuffer), you can do this more efficiently. But the purpose of this is to see how looping over a string works.
Nested loops. There are two nested loops in the is program, the outer while loop reads more input. The inner for loop gets every character from the input string starting at character 0.
Counting. A for loop is preferred to a while loop when counting through a series of numbers -- in this case all character positions in a string.
Equivalent. A for loop has the same condition as the equivalent while loop, but also incorporates an initialization, which would be before the while statement, and the increment, which would be at the end of the while body. You can write the loop either way, but putting the initialization, condition, and increment in one statement increases the readability.
Rate Tutorial: http://www.roseindia.net/java/java-tips/flow/loops/ex-string-reverse.shtml
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Example: String reverse
View Tutorial: Example: String reverse
Related
Tutorials:
|