Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package LabExam;
- import java.util.*;
- public class Recursion {
- /**
- * Given a string, compute recursively (no loops) a new string
- * where all the lowercase 'x' chars have been changed to 'xyx' chars.
- * <p>
- * changeXY("xyz123") → "xyxyz123"
- * changeXY("xxx") → "xyxxyxxyx"
- * changeXY("()") → "()"
- */
- public String changeXxyx(String str) {
- // TODO: Write a recursive function to complete this method
- //
- // Note: The code below is simply a placeholder to allow the
- // code to compile and run.
- String b;
- char a, d, c;
- a = 'x';
- d = 'y';
- c = 'x';
- StringBuilder sb = new StringBuilder();
- sb.append(a);
- sb.append(d);
- sb.append(c);
- b = sb.toString();
- if (str.equals(""))
- return "";
- char r = str.charAt(0);
- return (r == 'x' ? b :r) + changeXxyx(str.substring(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement