java - Find the longest word in a string recursively -


How to find the longest string in the string?

EDIT

Finished, thanks to everyone, here is the modified code Public Static String The Longest Term (String Sentence) {The Longest String; Int i = sentence. Index (''); If (i == -1) {return statement; } String first = sentence. Substring (0, i); First = first.trim (); String left = sentence. Substring (i); Rest = rest.trim (); Longest = string compaire (first, longest) (left)); Oldest return; }

First of all, we believe that there is no major word string argument Or the following spaces you are calling it trim () for a recurring case that is sensible.

Then we need to define two cases, base case and recursive case.

There is a case of grounds where no place is found, i.e. this sentence is the same word. In this case only return the sentence.

In the recursive case, you have given us the first word and the rest as you did. Make the longest call on the remaining sentence. Then return to the longest of the first word and whatever was returned by your recursive call

Comments