Java String Manipulation Study Guide
Table of Contents
1. Reverse a String
2. Check if Palindrome
Java String Manipulation Study Guide
1. Reverse a String
Description: Reverse a given string without using built-in reverse methods.
Interview Insight: Often used to test basic understanding of strings and loops.
public class ReverseString {
public static String reverse(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(reverse("hello")); // "olleh"
}
}
Your Notes: ____________________________
____________________________
Java String Manipulation Study Guide
2. Check if Palindrome
Description: Check if a string is a palindrome by comparing characters from both ends.
Interview Insight: Common warm-up interview question for string logic.
public class PalindromeCheck {
public static boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPalindrome("madam")); // true
}
}
Your Notes: ____________________________
____________________________