-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.java
More file actions
43 lines (37 loc) · 1.15 KB
/
palindrome.java
File metadata and controls
43 lines (37 loc) · 1.15 KB
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
39
40
41
42
43
public class palindrome {
public static void main(String[] args){
//decalre our palindrome
String palindrome = "No lemon,no melon";
//get its length
int len = palindrome.length();
/*
* We need to divide up our palindrome from a sequence of Strings
* To an Array of characters.
* So we need a temporary array to allow us to loop through each
* character of the palindrome on its own.
* This array has to be the same size as our palindrome
* */
char[] tempArr = new char[len];
/*
* We also need another array that will host our reversed String
* */
char[] reverseArr = new char[len];
/*
* next we loop through each character in the palindrome string and append
* each character into our temp array
* We will use a method called charAt(i)
* charAt(i), returns the ith character in the string, counting from 0.
* */
for(int i=0; i < len; i++){
tempArr[i] = palindrome.charAt(i);
}
/*
* Next lets reverse the array
* */
for(int j=0; j < len; j++){
reverseArr[j] = tempArr[len-1-j];
}
String reversedPalindrome = new String(reverseArr);
System.out.println(reversedPalindrome);
}
}