Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit f1c47f8

Browse files
author
Ahmed Tawila
committed
reverse string optimization
1 parent dc78c90 commit f1c47f8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/main/java/javarevisited/top/fifty/java/programs/from/coding/interviews/ReverseAString.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
public class ReverseAString {
44

55
public static String reverseString(String s) {
6-
String result = "";
6+
StringBuilder result = new StringBuilder();
77
for (int i = 0; i < s.length(); i++) {
8-
result += s.charAt(s.length() - i - 1);
8+
result.append(s.charAt(s.length() - i - 1));
99
}
10-
return result;
10+
return result.toString();
1111
}
12-
12+
1313
public static void main(String[] args) {
1414
String test = "To be reversed!";
1515
System.out.println(reverseString(test));

0 commit comments

Comments
 (0)