A month or so ago - when I was a wee level 9 - I tried and failed to do this task despite the output being correct in every possible iteration I could think of. With more experience I've returned to this indissoluble problem and used some slightly cleaner code. Again, the code returns the desired output on every possible string - including those with multiple spaces between them - but still does not verify. I would love to see an example string in which my code fails to produce the desired output.
99% of the time I understand that there's usually an overlooked flaw in my code, but this time it feels like I'm being failed for a reason I honestly can't fathom. Any help?
package com.codegym.task.task08.task0823;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Going national
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
s = s.replaceAll("\\s+", " ");
String[] array = s.split(" ");
for(int i = 0; i < array.length; i++){
String str = array[i];
array[i] = str.substring(0, 1).toUpperCase() + str.substring(1);
}
StringBuilder str = new StringBuilder();
for(int i = 0; i < array.length; i++){
str.append(array[i]);
str.append(" ");
}
String polishedString = str.toString().trim();
System.out.println(polishedString);
}
}