Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Programming challenge description:
- Given any string as an input, spell it out in reverse order, lower-case it and separate each character by hyphen.
- The output string should contain only letters and numbers. You should ignore all other non-alphanumeric characters.
- Input:
- Any string (eg: “butterfly”). You can assume that the input will never be null.
- Output:
- Reversed order of the input string (eg: "y-l-f-r-e-t-t-u-b")
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- public class Main {
- /**
- * Iterate through each line of input.
- */
- public static void main(String[] args) throws IOException {
- InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
- BufferedReader in = new BufferedReader(reader);
- String line;
- while ((line = in.readLine()) != null) {
- Main.reverseSpell(line);
- }
- }
- public static void reverseSpell(String input) {
- char[] stringAsChars = input.toCharArray();
- String reversed = "";
- for(int i = stringAsChars.length - 1; i >= 0; i--) {
- char elem = stringAsChars[i];
- if(!isLetterOrDigit(elem))
- continue;
- elem = toLower(elem);
- if(i != 0)
- reversed += elem + "-";
- else
- reversed += elem;
- }
- System.out.println(reversed);
- }
- private static boolean isLetterOrDigit(char elem) {
- return Character.isLetterOrDigit(elem);
- }
- private static char toLower(char elem) {
- return Character.toLowerCase(elem);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment