Array and String Manipulation Problems
Published: (December 12, 2025 at 03:59 PM EST)
2 min read
Source: Dev.to
Source: Dev.to
Reverse an array using another array
class Main {
public static void main(String[] args) {
int[] num = {10, 11, 12, 13, 14, 15, 16};
System.out.println("Original Array :");
for (int i = 0; i = 0; i--) {
result[j++] = num[i];
}
System.out.println();
System.out.println("Reversed Array :");
// print the resultant array
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
}
Reverse an array in‑place (using swapping)
class Main {
public static void main(String[] args) {
int[] num = {10, 11, 12, 13, 14, 15, 16};
System.out.println("Original Array :");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
System.out.println();
int left = 0;
int right = num.length - 1;
while (left < right) {
// swap without a temporary variable
num[left] = (num[left] + num[right]) - (num[right] = num[left]);
left++;
right--;
}
System.out.println("Reversed Array :");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}
}
Check if a string is a palindrome
A palindrome is a string that reads the same forward and backward.
class Main {
public static void main(String[] args) {
String str = "abcdcba";
int left = 0;
int right = str.length() - 1;
boolean flag = true;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
flag = false;
break;
}
left++;
right--;
}
if (flag) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}