bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Reverse a String

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Reverse a String?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ originalStr = "Hello";

Reverse a String

You can easily reverse a string by characters with the following example:

Example

String originalStr = "Hello";
String reversedStr = "";
for (int i = 0; i < originalStr.length(); i++) {
  reversedStr = originalStr.charAt(i) + reversedStr;
}
System.out.println("Reversed string: "+ reversedStr);

Explanation: We start with an empty string reversedStr . - On each loop, we take one character from the original string using charAt() . - Instead of adding it to the end, we place it in front of the existing reversedStr . - This way, the characters are built in reverse order. For example, from "Hello" we get "olleH" .

Related Pages

Java String Methods

Previous

Java How To Count Digits in a String

Next

Java How To - Palindrome Check