191,528 views
37 votes
37 votes
Write a recursive method that receives a string as a parameter and recursively capitalizes each character in the string (change a small cap to a large cap, e.g. a to A, b to B, etc) as following: capitalize the first letter from the string, then calls the function recursively for the remainder of the string and in the end, build the capitalized string. For example, for "java", will capitalize the first letter to "J"(base case), calls the method recursively for the reminder of the string "ava" (reduction), and after recursion/reduction is over it and returns "AVA" will build a new string from "J" and "AVA" and return "JAVA" to the calling method.

Write this algorithms in java source code plz.

User Mark Jay
by
2.7k points

1 Answer

17 votes
17 votes

Answer:

String words[]=str.split("\\s");

String capitalizeWord="";

for(String w:words){

String first=w.substring(0,1);

String afterfirst=w.substring(1);

capitalizeWord+=first.toUpperCase()+afterfirst+" ";

}

return capitalizeWord.trim();

Step-by-step explanation:

Define the word you are trying to capitalize and split it into an array.

String words[]=str.split("\\s");

Create a string for the capital output to be created as

String capitalizeWord="";

Capitalize the word using the "first.toUpperCase()" and "afterfirst" functions

for(String w:words){

String first=w.substring(0,1);

String afterfirst=w.substring(1);

capitalizeWord+=first.toUpperCase()+afterfirst+" ";

}

User Lourens
by
3.0k points