54 lines
850 B
Plaintext
54 lines
850 B
Plaintext
|
|
String reverse(String s) {
|
||
|
|
String result = "";
|
||
|
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||
|
|
result = result + s.charAt(i);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
reverse("hello")
|
||
|
|
reverse("Rava")
|
||
|
|
|
||
|
|
boolean isPalindrome(String s) {
|
||
|
|
String lower = s.toLowerCase();
|
||
|
|
int left = 0;
|
||
|
|
int right = lower.length() - 1;
|
||
|
|
while (left < right) {
|
||
|
|
if (lower.charAt(left) != lower.charAt(right)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
left++;
|
||
|
|
right--;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
isPalindrome("radar")
|
||
|
|
isPalindrome("hello")
|
||
|
|
isPalindrome("Racecar")
|
||
|
|
|
||
|
|
int countChar(String s, char c) {
|
||
|
|
int count = 0;
|
||
|
|
for (int i = 0; i < s.length(); i++) {
|
||
|
|
if (s.charAt(i) == c) count++;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
countChar("mississippi", 's')
|
||
|
|
countChar("hello world", 'l')
|
||
|
|
|
||
|
|
String repeat(String s, int n) {
|
||
|
|
String result = "";
|
||
|
|
for (int i = 0; i < n; i++) {
|
||
|
|
result = result + s;
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
repeat("ab", 5)
|
||
|
|
repeat("*", 10)
|
||
|
|
|
||
|
|
%methods
|
||
|
|
%quit
|