Question: Need help with this in Java 8 1 2 3 Task 1 There is a forum that has a limit of K characters per entry.


Need help with this in Java 8
1 2 3 Task 1 There is a forum that has a limit of K characters per entry. In this task your job is to implement an algorithm for cropping messages that are too long. You are given a message, consisting of English alphabet letters and spaces, that might be longer than the limit. Your algorithm should crop a number of words from the end of the message, keeping in mind that: it may not crop away part of a word; the output message may not end with a space; the output message may not exceed the K-character limit; the output message should be as long as possible. Java 8 This means that, in some cases, the algorithm may need to crop away the entire message, outputting an empty string. For example, given the text: "Codility We test coders" With K = 14 the algorithm should output: "Codility We" Note that: the output "Codility We te" would be incorrect, because the original message is cropped through the middle of a word; the output "Codility We would be incorrect, because it ends with a space; the output "Codility We test coders" would incorrect, because it exceeds the K-character limit; the output "Codility" would be incorrect, because it is shorter than the correct output. Write a function class Solution { public String solution (String message, int K); } which, given a message and an integer K, returns the message cropped to no more than K characters, as described above. Examples: 1. Given message = "Codility We test coders" and K = 14, the function should return "Codility We". Autocomplete is connected | All changes saved Files task1 solution.java test-input.txt Test Output 1 error || Detected some errors. solution.java x // you can also use imports, for example: // import java.util.*; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public String solution (String message, int K) { K = 14; System.out.println ("Codility We test coders"); } } Compiler output: Solution.java:13: error: reached end of file while parsing To leave editor use Ctrl + M Any problems with the editor? Switch to basic editor > Run Code Give Feedback 6:14 PM
Step by Step Solution
There are 3 Steps involved in it
The method solutionmessage k which takes the message and the k and then returns the substring from 0 ... View full answer
Get step-by-step solutions from verified subject matter experts
