Hi I have a code that checks if a string is palindrome or not.the code is like this:
package ProjeTarahi;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.String;
public class Main
{
public boolean CheckIsSymmetric(String s)
{
if (s.length()<=1)
{
return true;
}
if (s.charAt(0)==s.charAt(s.length()-1))
{
String sub = s.substring(1,s.length()-2);
return CheckIsSymmetric(sub);
}
else
{
return false;
}
}
public static void main(String args[])throws FileNotFoundException
{
Scanner sc=new Scanner(new FileInputStream(new File("in.txt")));
String input=sc.nextLine();
Main p=new Main();
if(p.CheckIsSymmetric(input)==true)
{
System.out.println("in reshte motegharen ast");
}
else
{
System.out.println("infinite");
}
}
}
I have written a code in c# that is exactly the same as the code above and its working very well but its not working properly in java and its output is always infinite. I stepped over my code and i think the problem is in the first if-statement of the CheckSymmetric() and it always jumps it but i don't know why.anybody can help me plz?
Surprisingly this type of question appear a lot in Computer-Science related classes. Here is my version of finding palindrome of a String. This version is design to strip away any non-word characters and reduce every character to lower case. It also work with numbers as it will just compare it as a String. It's only 13 lines long with a main and a Boolean method. No for loop, substring or anything.
Hope I help! -Cheers
p/s: I apologize for my style of indentation :D
I used the StringBuffer integrated class of Java and the meta character "/W" (all non-word characters, must be capital W). Please feel free to criticize or discuss!