The code works perfectly with multiple space, you input
sam i am.
and it gives the proper output. I am pretty sure my code works fine so must be an error in the checker. Any idea?
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();
boolean toCap = true;
//write your code here
for (int i = 1; i < s.length(); i++) {
String sp = s.substring(i-1,i);
if (toCap){
s = s.substring(0,i-1) + sp.toUpperCase() + s.substring(i,s.length());
toCap = false;
}
if (sp.equals(" ")){
toCap = true;
}
}
System.out.println(s);
}
}
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();
boolean toCap = true;
//write your code here
for (int i = 1; i < s.length(); i++) {
String sp = s.substring(i-1,i);
if (toCap){
s = s.substring(0,i-1) + sp.toUpperCase() + s.substring(i,s.length());
toCap = false;
}
if (sp.equals(" ")){
toCap = true;
}
}
System.out.println(s);
}
}