944,198 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 11508
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 20th, 2007
0

check whether a palindrome can be formed from a given stirng

Expand Post »
how can i check a given string to form a palindrome or not
for(i=0;i<str.length();i++)
{
if(str.charAt(i)==str1.charAt(i)
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
May 20th, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

Hmm whats str1?
You have to compare the first letter and the last letter, then the second letter and the last but one letter and so on. So...think how we could go about doing that.
Reputation Points: 10
Solved Threads: 0
Light Poster
jetru is offline Offline
27 posts
since Jun 2006
May 20th, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

Assuming your palindrome logic ignores case, here is one solution:

java Syntax (Toggle Plain Text)
  1. String str = "AB , ba";
  2. StringBuffer sb = new StringBuffer(str).reverse();
  3. String strRev = sb.toString();
  4. if(str.equalsIgnoreCase(strRev))
  5. System.out.println("Palindrome");
  6. else
  7. System.out.println("Not a Palindrome");
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 20th, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

sorry i am not trying to find the given string is palindrome or not,i am trying to find from a given string is it possible to form a palindrome or not?
suppose u input "oppa"
from this string it is possible to from a palindrome "pop"
but if u input a string"opai"
it is not possible to form a palindrome.that is what i want please if u know tell me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
May 20th, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

You can start off by eliminating words which don't have a repeated character. This would help you in cutting down the processing. Then with the words which have at least a pair of repeating characters, try out various combinations of the character forming the word.

For eg. if your word is "sat", you can safely eliminate it since it doesn't have a repeating character. Now if the word in consideration is "sosa", generate various combinations of the characters forming the word which has at least a pair of repeating characters. Some examples would be "sos" and "sas". Of course yo have to keep in mind that single characters are by default palindromes.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 21st, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

Click to Expand / Collapse  Quote originally posted by pavani2006 ...
sorry i am not trying to find the given string is palindrome or not,i am trying to find from a given string is it possible to form a palindrome or not?
suppose u input "oppa"
from this string it is possible to from a palindrome "pop"
but if u input a string"opai"
it is not possible to form a palindrome.that is what i want please if u know tell me
what exactly are you trying to do???
"pop" is in no way a palindrome to "oppa".
a palindrome to "oppa" would be "appo".

you are here asking, is it possible to find a palindrome for "opai" with boolean 'false' as return.
well... the palindrome to "opai" is "iapo".

so, if I'm not mistaking, you're trying to find a word that is a palindrome and that can be formed with the letters used in the original (input)String?
Last edited by stultuske; May 21st, 2007 at 5:11 am.
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
May 21st, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

Click to Expand / Collapse  Quote originally posted by stultuske ...
well... the palindrome to "opai" is "iapo".
brrrr... by my last post you can propably see I didn't sleep to well ...
opai is no palindrome, excuse me for the mistake
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
May 21st, 2007
0

Re: check whether a palindrome can b eformed from a given stirng

Click to Expand / Collapse  Quote originally posted by stultuske ...
what exactly are you trying to do???
"pop" is in no way a palindrome to "oppa".
a palindrome to "oppa" would be "appo".

you are here asking, is it possible to find a palindrome for "opai" with boolean 'false' as return.
well... the palindrome to "opai" is "iapo".

so, if I'm not mistaking, you're trying to find a word that is a palindrome and that can be formed with the letters used in the original (input)String?
i am not trying to find i am trying to form a palindrome fron the letters of the given string
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
May 21st, 2007
0

Re: check whether a palindrome can be formed from a given stirng

> i am not trying to find i am trying to form a palindrome fron the letters of the given string
Use the approach suggested by me in my previous post.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 25th, 2007
0

Re: check whether a palindrome can be formed from a given stirng

hey i think what you can do is you find the frequency of characters in a word
Like ""madam" is having 2 m and 2 "a" and 1"d" so u can make pallindronme for it

Its just a logic that u can think abt...Hope it will help u in 1 way or another
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
staneja is offline Offline
63 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: NullPointer Exception
Next Thread in Java Forum Timeline: Print Coin toss result every 30 tosses for 500 times





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC