• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finding half palindrome words

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm a newbie.
I managed to get a code that checks for palindrome words or strings.
A palindrome is a word that reads the same forwards and backwards. Like "redevider".
Now...How do I check for a HALF palindrome word or string? for example, "redivadar" is half palindrome because 3 out of 5 characters match. "redivaaar" is not even half palindrome because only 2 out of 5 chars match.
below is the code that checks for FULL palindromes.
Any ideas on how to tweak the code to check for HALF palindromes?

 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to Programming Diversions.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi El,
Try this
/*
* Palindrome.java
*
* Created on October 8, 2003, 2:27 PM
*/
/**
*
* @author Vinod K. Chandana
*/
public class Palindrome
{
public static void main (String[ ] args)
{
String inputSentence;
inputSentence = "redivadar";
int matches = 0;
int length = inputSentence.length() - 1;
int mid = (length + 1)/2;
for(int i = 0; i < mid; i++){
if(inputSentence.charAt(i) == inputSentence.charAt(length - i))
matches++;
}
if(matches < (mid/2))
System.out.println (inputSentence + " is NOT a palindrome.");
else if(matches == mid)
System.out.println (inputSentence + " is a palindrome.");
else if(matches == (mid/2))
System.out.println (inputSentence + " is a half palindrome.");
}
}
Regards,
Vinod

Originally posted by el chupacabra:
Hi all, I'm a newbie.
I managed to get a code that checks for palindrome words or strings.
A palindrome is a word that reads the same forwards and backwards. Like "redevider".
Now...How do I check for a HALF palindrome word or string? for example, "redivadar" is half palindrome because 3 out of 5 characters match. "redivaaar" is not even half palindrome because only 2 out of 5 chars match.
below is the code that checks for FULL palindromes.
Any ideas on how to tweak the code to check for HALF palindromes?

 
justin marshall
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you thank you thank you.
I got the concept and the sample code is great!!
el chupacabra.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great nickname, El Chupacabra!
But please adjust your displayed name to meet the JavaRanch Naming Policy. You can do so here.
Have fun on the 'Ranch. (We got some cattle and moose round these parts, but not too many goats.)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just curious, does that algorithm work equally well for palindromes with even number letters an odd number letters? BOB and BOOB? Would left>=right instead of left>right end differently?
 
reply
    Bookmark Topic Watch Topic
  • New Topic