Guest User

Untitled

a guest
Sep 19th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*
  2. Programming challenge description:
  3. Given any string as an input, spell it out in reverse order, lower-case it and separate each character by hyphen.
  4.  
  5. The output string should contain only letters and numbers. You should ignore all other non-alphanumeric characters.
  6.  
  7. Input:
  8. Any string (eg: “butterfly”). You can assume that the input will never be null.
  9.  
  10. Output:
  11. Reversed order of the input string (eg: "y-l-f-r-e-t-t-u-b")
  12. */
  13.  
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.IOException;
  17. import java.io.InputStreamReader;
  18. import java.nio.charset.StandardCharsets;
  19.  
  20. public class Main {
  21. /**
  22. * Iterate through each line of input.
  23. */
  24. public static void main(String[] args) throws IOException {
  25. InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
  26. BufferedReader in = new BufferedReader(reader);
  27. String line;
  28. while ((line = in.readLine()) != null) {
  29. Main.reverseSpell(line);
  30. }
  31. }
  32.  
  33. public static void reverseSpell(String input) {
  34. char[] stringAsChars = input.toCharArray();
  35. String reversed = "";
  36.  
  37. for(int i = stringAsChars.length - 1; i >= 0; i--) {
  38. char elem = stringAsChars[i];
  39. if(!isLetterOrDigit(elem))
  40. continue;
  41.  
  42. elem = toLower(elem);
  43.  
  44. if(i != 0)
  45. reversed += elem + "-";
  46. else
  47. reversed += elem;
  48. }
  49.  
  50. System.out.println(reversed);
  51. }
  52.  
  53. private static boolean isLetterOrDigit(char elem) {
  54. return Character.isLetterOrDigit(elem);
  55. }
  56.  
  57. private static char toLower(char elem) {
  58. return Character.toLowerCase(elem);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment