You are on page 1of 1

CS 2110 SP 2012 Self Knowledge Assessment on Recursion. 1) My String reverser has gone awry!

I wanted to write a method that would reverse a String but I seem to have gotten something wrong. Somebody help and fix this!

public static String reverse(String s){ return s.charAt(s.length() - 1) + reverse(s.substring(0, s.length() - 1)); }

2) Ok, now that youve fixed my String reverser, perhaps you can help me improve it. Make a recursive string reverser

that reverses each word in a String without reversing the whole String. Eg: First Spotify then Sudoku what will be our next project? => tsriF yfitopS neht ukoduS tahw lliw eb ruo txen tcejorp Each word is individually reversed. Dont use anything that turns your String into an array of Strings split by spaces. Lets say the most advanced thing you can use is indexOf. An elegant solution will use reverse() in it. A lot of recursive functions follow similar patterns so get familiar with them. One solution looks like reverse().

3) Write a method to recursively print out rows of Pascals triangle >pascal(5)


1 1 1 1 1 1 2 3 4

1 3 6

1 4

Hint: Think of it row by row. And consider dummy nodes. That is, if our first row is [ 0 1 0 ] we can define our second row as: for( from 1 to secondRowLen){ secondRow[i] = firstRow[ i 1] + firstRow[i]} [0 1 1 0] and the row afterwards can be defined in the same way: [0 1 2 1 0]

You might also like