Skip to content

Commit 602aa74

Browse files
committed
Solve java reverse string
1 parent c8e07fd commit 602aa74

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
String word = sc.next();
8+
sc.close();
9+
10+
System.out.println(isPalindrome(word) ? "Yes" : "No");
11+
12+
}
13+
14+
private static boolean isPalindrome(String word) {
15+
return word.equals(reverse(word));
16+
}
17+
18+
// Copied from
19+
// https://www.baeldung.com/java-reverse-string
20+
private static String reverse(String word) {
21+
if (word == null) {
22+
return word;
23+
}
24+
25+
String output = "";
26+
27+
for (int i = word.length() - 1; i >= 0; i--) {
28+
output = output + word.charAt(i);
29+
}
30+
31+
return output;
32+
}
33+
}

0 commit comments

Comments
 (0)