Well, I guess the problem occurs when input have more than one space next to each other, but I think that condition:
if (y.charAt(i) == ' ' && y.charAt(i + 1) == ' ') {
continue;
}
should help me with that.
Why no?
Here is all of my code:
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
if (s.contains(" ")) {
String[] h = s.split(" ");
if (!(' ' == s.charAt(0))) {
for (int i = 0; i < h.length; i++) {
String y = h[i];
if (y.charAt(i) == ' ' && y.charAt(i + 1) == ' ') {
continue;
}
String c = y.substring(0, 1).toUpperCase() + y.substring(1);
System.out.print(c + " ");
}
} else {
for (int i = 1; i < h.length; i++) {
String y = h[i];
if (y.charAt(i) == ' ' && y.charAt(i + 1) == ' ') {
continue;
}
String c = y.substring(0, 1).toUpperCase() + y.substring(1);
System.out.print(c + " ");
}
}
} else {
String wynik = s.substring(0, 1).toUpperCase() + s.substring(1);
System.out.println(wynik);
}
}
}
Thanks in advance!