Important alert: (current site time 7/15/2013 3:45:32 PM EDT)
 

VB icon

Check string... can u spell it both ways?

Email
Submitted on: 9/25/2000 7:07:04 PM
By: vefari  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 9449
author picture
(About the author)
 
     This is something useful (I hope). It checks if a string that you provide can be read both ways. It shows how to use recursive functions.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Check string... can u spell it both ways?
// Description:This is something useful (I hope). It checks if a string that you provide can be read both ways. It shows how to use recursive functions.
// By: vefari
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1968&lngWId=2//for details.//**************************************

package palindroma;
import java.lang.String;
import java.io.*;
public class myPalindr
{
 public myPalindr()
 {
 }
//******************************************************************************
// checkString is a recursive function that runs through the string provided
// as argument and checks if the first and last letters are the same. If that
// is true then it makes a substring out of the original one (cutting away the
// first and last letters and finally calls it self again with the substring as
// argument. Easy right? BTW.. you can use this for numbers as well...
//******************************************************************************
 public boolean checkString(String s)
 {
if(s.length() > 2)
{
if(s.charAt(0) == s.charAt(s.length()-1))
{
String t;
t = s.substring(1, s.length()-1);
return checkString(t);
}
else
return false;
}
if(s.length()==1)
return true;
if(s.length()==2)
{
if(s.charAt(0) == s.charAt(1))
return true;
else
return false;
}
else
return false;
 }
//******************************************************************************
// You must provide a string to check... Enjoy...
//******************************************************************************
 public static void main(String[] args)
 {
 BufferedReader bLineIn = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("\nEnter a string: ");
 String sCheck = null;
try
{
 sCheck = bLineIn.readLine();
}
catch(IOException e)
{
 System.out.println(e);
}
if(sCheck.length() >=1)
{
 String str = "";
 for(int i = 0; i < sCheck.length(); i++)
 {
 str += sCheck.charAt(i);
 }
 myPalindr p = new myPalindr();
 if(p.checkString(str))
 {
 System.out.println("\nThe string " + str + " is palindroma");
 }
 else
 System.out.println("\nThe string " + str + " is not palindroma");
}
else
 System.out.println("ups.. something happened...");
 }
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/16/2002 5:10:14 AM鋭縦

けいしぉ
(If this comment was disrespectful, please report it.)

 
2/22/2005 12:52:37 AM

very elusive, clever and cunning. thats how it should be. great code. many people would have done it by ripping the word letter by letter....
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.