class RevString
{
static String reverse(String str) {
char[] c = str.toCharArray();
char[] r = new char[c.length];
int end = c.length - 1;
for (int n = 0; n <= end; n++) {
r[n] = c[end - n];
}
return new String(r);
}
bool isPalindrome(string str) {
int end= str.length();
for (int i=0;i<(end/ 2)+ 1;++i) {
if (str.charAt(i) != str.charAt(end- i - 1)) {
return false;
}
}
return true;
}
public static void main (String[] args)
{
System.out.println(RevString.reverse("hello"));
System.out.println(RevString.isPalindrome("liril"));
}
}
{
static String reverse(String str) {
char[] c = str.toCharArray();
char[] r = new char[c.length];
int end = c.length - 1;
for (int n = 0; n <= end; n++) {
r[n] = c[end - n];
}
return new String(r);
}
bool isPalindrome(string str) {
int end= str.length();
for (int i=0;i<(end/ 2)+ 1;++i) {
if (str.charAt(i) != str.charAt(end- i - 1)) {
return false;
}
}
return true;
}
public static void main (String[] args)
{
System.out.println(RevString.reverse("hello"));
System.out.println(RevString.isPalindrome("liril"));
}
}
We can do it one another way :
ReplyDeletepublic String reverse(String str){
if (str.length() == 0)
return str;
return reverse(str.substring(1)) + str.charAt(0);
}