/** * This programm takes a word from the commandline, reverses it * ans prints it back to the terminal. * * @author felixf * */ public class Palindrom { public static void main(String[] args) { String word = getWordFromCommandline(args); /* * The word should not be empty. */ if (word.equals("")) { System.exit(1); // This statement stops a programm } System.out.println("You gave the word: " + word); char[] wordAsArray = string2array(word); char[] wordAsArrayReversed = reverseArray(wordAsArray); String reversedWord = array2string(wordAsArrayReversed); System.out.println("The reversed word is: " + reversedWord); } /** * This method takes the argument-array, provided by the main method * and returns the word given as paramenter. * In the case the is no word, the method prints an error-message to * the console. * * @param args the argument-array * @return returns the word parsed form the commandline. */ public static String getWordFromCommandline(String[] args) { /* * If the length of the argument-array is not 1, there was no word * given on the commandline. */ if (args.length == 1) { return args[0]; } /* * If the length of the args array is longer than 1, there was more * than one word given on the commandline. * Because we only want to reverse one word, we ignore the others, and * take the first given word. */ else if (args.length > 1) { System.out.println("You gave more than one word, we only take the first one.\n"); return args[0]; } else { System.out.println("There was no word given."); System.out.println("Usage:"); System.out.println("\t # java Palindrom "); return ""; } } /** * This method converts a String to an array of chars. * * @param word the word * @return a array of chars representing the word */ public static char[] string2array(String word) { int wordLength = word.length(); /* * The array should be as long as the word itself. */ char[] result = new char[wordLength]; /* * */ for (int i = 0; i < result.length; i++) { result[i] = word.charAt(i); } return result; } /** * This method takes an array of chars, reverses it, and returns it. * * @param arrayToReverse the array you want to reverse. * @return the reversed array. */ public static char[] reverseArray(char[] arrayToReverse) { char[] reversedArray = new char[arrayToReverse.length]; int currentIndexInReversedArray; for (int i = 0; i < arrayToReverse.length; i++) { /* * Here we calculate the index which iterates over the array in * which will stand the reversed word. * The 1 needs to be subtracted because if the indexes begin with 0. */ currentIndexInReversedArray = arrayToReverse.length - 1 - i; /* * Here we write the current char from the array we want to reverse to * the array which will contain the reversed word, when we have finished. */ reversedArray[currentIndexInReversedArray] = arrayToReverse[i]; } return reversedArray; } /** * Converts an array of chars to a String. * * @param array array of chars * @return a String of the chars of the given array */ public static String array2string(char[] array) { String result = ""; /* * This for-loop iterates over the given array and adds each * char to the string, which we want to give back. */ for (int i = 0; i < array.length; i++) { result = result + array[i]; // adding the current char } return result; } }