Write a recursive method to retrieve the number of strings

/* Write a recursive method to retrieve the number of strings that have exactly the same length provided in the parameter. This method cannot contain any loop (that is, uses of for, while, do while are prohibited). */

public static int countKLenghthStrings (StringNode M, int k){

/* Write your code here */

}

Solution

public static int countKLengthStrings(StringNode M,int k)

{

if(M==\"\") //If the Node is empty we are returning

return 0;

else if(M.length()==k) //If the string length is equal to k we are returning by adding 1 to it

return 1+countKLengthStrings(M-1,k);

else

return countKLengthStrings(M-1,k); //If not equal we are incrementing the value

}

/* Write a recursive method to retrieve the number of strings that have exactly the same length provided in the parameter. This method cannot contain any loop (

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site