import javax.swing.JOptionPane;
public class Palindrome{
public static void main(String[]args){
String input = JOptionPane.showInputDialog("enter the string");//string input
String input1 = input.toUpperCase();//converts the entered string to upper case
char input3 [] = input1.toCharArray();//put it in to an array
char input4[] = new char[99999999];//used for suffix
input4= input3;
int first=0;//first letter of the string
int last= input1.length()-1;//last letter
if(last<=0)//if the string is empty
{
JOptionPane.showMessageDialog(null, "Try again and enter a valid string");
return;
}
while (first<last)//goes to the middle letter
{
if(input1.charAt(first)!= input1.charAt(last))//if its not equal
{
System.out.println("The string is not a palindrome");
System.out.print("suffix: "+input4[0]);
for(int x=last;x>=1;x--)
{
System.out.print(input4[x]);
}
System.out.println();
System.out.print("backwards is: ");
for(int i =last;i>=0;i--)
{
System.out.print(input3[i]);
}
return;
}
else
{
System.out.println("The string is a palindrome");
System.out.print("suffix: "+input4[0]);
for(int x=last;x>=1;x--)
{
System.out.print(input4[x]);
}
System.out.println();
System.out.print("backwards is: ");
for(int i =last;i>=0;i--)
{
System.out.print(input3[i]);
}
}
}
}
}
Palindrome JAVA
Page 1 of 113 Replies - 4960 Views - Last Post: 16 October 2010 - 08:00 PM
#1
Palindrome JAVA
Posted 14 October 2010 - 08:53 AM
Replies To: Palindrome JAVA
#2
Re: Palindrome JAVA
Posted 14 October 2010 - 09:23 AM
for(int i = 0; i < myStr.length()/2; i++){
//if the charAt(i) != the charAt(myStr.length()-1-i)
//then you can terminate the loop, as you do not have a palindrome
}
//if you get through the loop without terminating
//then myStr is a palindrome
#3
Re: Palindrome JAVA
Posted 14 October 2010 - 09:24 AM
#4
Re: Palindrome JAVA
Posted 14 October 2010 - 09:28 AM
String string = someInput;
//we begin by assuming it is
boolean isPal = true;
//while true and not in the middle
while(isPal && (first < last)){
if(string.charAt(first) == string.chatAt(last)){
//will check the next two chars next time around
first++;
last--;
//still true
isPal = true;
}
//otherwise, we break the loop
else{
isPal = false;
}
System.out.println( "Is it a palindrome?: " + isPal);
this is the basic structure and you can spiffy it up with your error checking statements yourself.
to print it backwards, you just have to run a for loop in reverse order, printing .charAt(length - 1) to .charAt(0). A little more concise, eh? You have the right train of thought though--I just don't like using unnecessary char arrays
#5
Re: Palindrome JAVA
Posted 14 October 2010 - 03:13 PM
public class palindrome1 {
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("enter the string");//string input
String input1 = input.toUpperCase();//converts the entered string to upper case
for(int i = 0; i < input1.length()/2; i++){
if (input1.charAt(i) != input1.charAt(input1.length()-1-i))
{
System.out.println("the entered string is not a palindrome");
break;
}
else
{
System.out.print("the entered string is a palindrome");
break;
}
}
}
}
This post has been edited by cyberbemon: 14 October 2010 - 03:14 PM
#6
Re: Palindrome JAVA
Posted 14 October 2010 - 05:03 PM
import javax.swing.JOptionPane;
public class Pali{
public static void main (String[] args) {
String orig = JOptionPane.showInputDialog("enter the string");//string input
String flipped = reversed(orig);
if(orig.equals(flipped))
System.out.println("This IS a palindrome.");
else
System.out.println("This IS NOT palindrome.");
System.out.print(orig.charAt(0));
System.out.print(flipped.substring(0, flipped.length() - 1));
}
private static String reversed(String str)
{
return (new StringBuffer(str)).reverse().toString();
}
}
#7
Re: Palindrome JAVA
Posted 14 October 2010 - 05:21 PM
#8
Re: Palindrome JAVA
Posted 14 October 2010 - 05:22 PM
cyberbemon, on 14 October 2010 - 04:13 PM, said:
public class palindrome1 {
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("enter the string");//string input
String input1 = input.toUpperCase();//converts the entered string to upper case
for(int i = 0; i < input1.length()/2; i++){
if (input1.charAt(i) != input1.charAt(input1.length()-1-i))
{
System.out.println("the entered string is not a palindrome");
break;
}
else
{
System.out.print("the entered string is a palindrome");
break;
}
}
}
}
You almost dit it ...but you cannot apply the if() else inside the loop it will break at the firs iteration whatever happens..
public class palindrome1 {
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("enter the string");//string input
String input1 = input.toUpperCase();//converts the entered string to upper case
boolean palin = true;
for(int i = 0; i < input1.length()/2; i++){
if (input1.charAt(i) != input1.charAt(input1.length()-1-i))
{
palin = false;
break; // no need to continue the loop
}
}
if(palin)
System.out.print("the entered string is a palindrome");
else
System.out.print("the entered string is NOT a palindrome");
}
}
#9
Re: Palindrome JAVA
Posted 14 October 2010 - 05:52 PM
macosxnerd101, on 14 October 2010 - 04:21 PM, said:
Good point!
import javax.swing.JOptionPane;
public class Pali{
public static void main (String[] args) {
String orig = JOptionPane.showInputDialog("enter the string").toUpperCase().replace(" ", "" );//string input
String flipped = reversed(orig).toUpperCase();
if(orig.equals(flipped))
System.out.println("This IS a palindrome.");
else
System.out.println("This IS NOT palindrome.");
System.out.print(orig.charAt(0));
System.out.print(flipped.substring(0, flipped.length() - 1));
}
private static String reversed(String str)
{
return (new StringBuilder(str)).reverse().toString();
}
}
#10
Re: Palindrome JAVA
Posted 16 October 2010 - 11:52 AM
once again thanks a lot
#11
Re: Palindrome JAVA
Posted 16 October 2010 - 11:55 AM
#12
Re: Palindrome JAVA
Posted 16 October 2010 - 12:10 PM
macosxnerd101, on 16 October 2010 - 10:55 AM, said:
thanks man
#13
Re: Palindrome JAVA
Posted 16 October 2010 - 12:56 PM
import java.util.Stack;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.List;
public class Plindrome
{
// instance variables
private Stack<Character> plindromeStack;
public boolean plindromeNumber;
/**
* Constructor for objects of class Palindrome
*/
public Plindrome()
{
// initialise instance variables
plindromeStack = new Stack<Character>();
plindromeNumber = false;
}
/**
* checking if the series of numbers are plindrome
*/
public String checkForPlindrome(String number)
{
String validPlindrome = "Not a plindrome";
String tempPlindrome = "";
for(int index = 0; index < number.length(); index++)
{
plindromeStack.push(number.charAt(index));
}
while(plindromeStack.size() != 0)
{
tempPlindrome += ""+plindromeStack.pop();
if(tempPlindrome.equals(number))
{
validPlindrome = "a plindrome";
plindromeNumber = true;
}
}
return validPlindrome;
}
}
#14
Re: Palindrome JAVA
Posted 16 October 2010 - 08:00 PM
harmy01, on 16 October 2010 - 01:56 PM, said:
import java.util.Stack;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.List;
public class Plindrome
{
// instance variables
private Stack<Character> plindromeStack;
public boolean plindromeNumber;
/**
* Constructor for objects of class Palindrome
*/
public Plindrome()
{
// initialise instance variables
plindromeStack = new Stack<Character>();
plindromeNumber = false;
}
/**
* checking if the series of numbers are plindrome
*/
public String checkForPlindrome(String number)
{
String validPlindrome = "Not a plindrome";
String tempPlindrome = "";
for(int index = 0; index < number.length(); index++)
{
plindromeStack.push(number.charAt(index));
}
while(plindromeStack.size() != 0)
{
tempPlindrome += ""+plindromeStack.pop();
if(tempPlindrome.equals(number))
{
validPlindrome = "a plindrome";
plindromeNumber = true;
}
}
return validPlindrome;
}
}
There are peoples who likes to complicate their life for nothing
Don't worry about them
@harmy01 this is obviously an homework for basic student in Java who probably does not even know what a stack is, and may be not even an Object
This post has been edited by pbl: 16 October 2010 - 08:09 PM
|
|

New Topic/Question
Reply




MultiQuote









|