fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static boolean isAlphabet(char x) {
  11. return ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'));
  12. }
  13.  
  14. static void reverse(char ch[]) {
  15.  
  16. int l = 0;
  17. int startIndex = 0;
  18. int endIndex = 0;
  19. while (l < ch.length - 1) {
  20. if (isAlphabet(ch[l])) {
  21. l++;
  22. } else {
  23. endIndex = l - 1;
  24. while (startIndex < endIndex){
  25. char temp = ch[startIndex];
  26. ch[startIndex] = ch[endIndex];
  27. ch[endIndex] = temp;
  28. endIndex--;
  29. startIndex++;
  30. }
  31. l++;
  32. startIndex = l;
  33. }
  34. }
  35. }
  36.  
  37. public static void main(String[] args) throws java.lang.Exception {
  38. String inputString = "comp!ex, ex.amp!e";
  39. String[] splitArray = inputString.split("\\s");
  40. char[] ch1 = splitArray[0].toCharArray();
  41. reverse(ch1);
  42. char[] ch2 = splitArray[1].toCharArray();
  43. reverse(ch2);
  44. StringBuilder reverseString = new StringBuilder();
  45. reverseString.append(ch1);
  46. reverseString.append(" ");
  47. reverseString.append(ch2);
  48. System.out.println(reverseString.toString());
  49. }
  50. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
pmoc!ex, xe.pma!e