Question

Possible Duplicate:
Programming java to determine a symmetrical word

am new here, but I am having hard time figuring out how to write a code to determine an input of word and see if the first is matching with the end of the word. You may input abba and get answer it's evenly symmetric and aba is oddly symmetric.

Please show me how:(

Just two main things.

first I want to know if it's oddly or evenly amount of letter(number of letter divided by 2,if it's ending with 0.5, it's oddly symmetric, if is an integer it's evenly symmetric.

second I want to get (i.e 1=n,2=n-1,3=n-2...) position of the letter in the word to be the main idea of the execution.If there is a last letter in the oddly symmetric word, ignore the last remaining letter.

I appreciate any headstart or idea:) Thanks!

Thanks KDiTraglia, I made the code and compiled and here is what I put. I am not getting any further.

Reported problem:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: reverse cannot be resolved or is not a field reverse cannot be resolved or is not a field Syntax error, insert ") Statement" to complete IfStatement

This is what i got from, KDiTraglia's help

    public class WordSymmetric {
public static void main(String[] args) {
 String word = "abccdccba";


if ( (word.length() % 2) == 1 ) {
    System.out.println("They are oddly symmetric");
    //odd
}
else {
    System.out.println("They are evenly symmetric");
    //even
}

int halfLength = word.length() / 2;
String firstHalf = word.substring(0, halfLength);
String secondHalf = word.substring(halfLength, word.length());

System.out.println(secondHalf.reverse());

if (firstHalf.equals(secondHalf.reverse()) {
    System.out.println("They match");
    //they match
} 
} }
Was it helpful?

Solution

The reverse() approach is very clean and readable. Unfortunately there is no reverse() method for Strings. So you would either have to take an external library (StringUtils from the appache common lang3 library has a reverse method) or code it yourself.

public static String reverse(String inputString) {
  StringBuilder reverseString = new StringBuilder();
  for(int i = inputString.length(); i > 0; --i) {
    char result = inputString.charAt(i-1);
    reverseString.append(result);
  }
  return reverseString.toString();
}

(This only works for characters that can fit into a char. So if you need something more general, you would have to expand it.)

Then you can just have a method like this:

enum ePalindromResult { NO_PALINDROM, PALINDROM_ODD, PALINDROM_EVEN };
public static ePalindromResult checkForPalindrom(String inputStr) {
  // this uses the org.apache.commons.lang3.StringUtils class: 
  if (inputStr.equals(StringUtils.reverse(inputStr)) {
    if (inputStr.length % 2 == 0) return PALINDROM_EVEN;
    else return PALINDROM_ODD;
  } else return NO_PALINDROM;
}

OTHER TIPS

String does not have a reverse method. You could use the apache commons lang library for this purpose:

http://commons.apache.org/lang/api-release/org/apache/commons/lang3/StringUtils.html#reverse%28java.lang.String%29

System.out.println(secondHalf.reverse());

There is no reverse() method defined fro String

I would probably loop over word from index 0 to the half (word.length() / 2) and compare the character at the current index (word.charAt(i)) with the correspoding from the other half (word.charAt(word.length() - i). This is just a rough draft, you probably need to think about the loop end index, depending on oddly or evenly symmetry.

You can adapt this :

    final char[] word = "abccdccba".toCharArray(); // work also with "abccccba"
    final int t = word.length;
    boolean ok = true;    
    for (int i = t / 2; i > 0; i--) {
        if (word[i - 1] != word[t - i]) {
            ok = false;
            break;
        }
        System.out.println(word[i - 1] + "\t" + word[word.length - i]);
    }
    System.out.println(ok);

Console :

c c
c c
b b
a a
true

Use class StringBuffer instead of String

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top