Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package LabExam;
  2. import java.util.*;
  3.  
  4. public class Recursion {
  5. /**
  6. * Given a string, compute recursively (no loops) a new string
  7. * where all the lowercase 'x' chars have been changed to 'xyx' chars.
  8. * <p>
  9. * changeXY("xyz123") → "xyxyz123"
  10. * changeXY("xxx") → "xyxxyxxyx"
  11. * changeXY("()") → "()"
  12. */
  13. public String changeXxyx(String str) {
  14.  
  15. // TODO: Write a recursive function to complete this method
  16. //
  17. // Note: The code below is simply a placeholder to allow the
  18. // code to compile and run.
  19.  
  20. String b;
  21.  
  22. char a, d, c;
  23. a = 'x';
  24. d = 'y';
  25. c = 'x';
  26.  
  27. StringBuilder sb = new StringBuilder();
  28. sb.append(a);
  29. sb.append(d);
  30. sb.append(c);
  31. b = sb.toString();
  32.  
  33. if (str.equals(""))
  34. return "";
  35. char r = str.charAt(0);
  36. return (r == 'x' ? b :r) + changeXxyx(str.substring(1));
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement