I've worked on this problem for a few hours now and I can't seem to find what the issue is. Whenever I test it, it will always come back correct but it will not validate and warns me of multiple spaces between words which I've tested for. Any recommendations on how to resolve this issue?
Additionally, I know that it isn't exactly an optimal solution, so any optimizations would be welcome.
package com.codegym.task.task08.task0823;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Going national
Write a program that reads a string from the keyboard.
The program should change the first letter of each word to uppercase.
Display the result on the screen.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
//write your code here
String allLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] allLet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
s = s.toLowerCase();
char space = ' ';
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == space) {
continue;
} else if (arr[i] == arr[0] || (allLetters.indexOf(arr[i-1]) == -1 && allLetters.indexOf(arr[i]) != -1)) {
arr[i] -=32;
}
}
String b = new String(arr);
System.out.println(b);
}
}