-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStrops.java
More file actions
38 lines (34 loc) · 782 Bytes
/
Strops.java
File metadata and controls
38 lines (34 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package strings;
public class Strops {
/**
* Reverses a string
*
* @param str The string to reverse.
* @return The reversed string.
*/
public String reverse(String str) {
// Use StringBuilder's built-in reverse method
return new StringBuilder(str).reverse().toString();
}
/**
* Checks if a string is a palindrome
*
* @param str The string to check.
* @return True if the string is a palindrome, false otherwise.
*/
public boolean isPalindrome(String str) {
if (str.length() == 0) {
return false;
}
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}