Skip to main content
added 441 characters in body
Source Link
Gilsham
  • 153
  • 3

You are checking the capital letters twice in your loop, you should increment ii by 1 when you add in a space. This will also mean you don't need to use i + 1i + 1 to de-capitalise the current letter

if(Character.isUpperCase(str.charAt(i)))
"andDad"
    ^== (i == 3)

str.insert(i, ' ');
"and Dad"
    ^=== (i == 3)

char ch = Character.toLowerCase(str.charAt(i+1)); 
str.setCharAt((i+1), ch);
"and dad"
     ^=== (i == 3) + 1
i++;
"and dad"
     ^=== (i == 4)

new while loop iteration
"and dad"
     ^=== (i == 4)

change the code in the if this

str.insert(i, ' '); 
i++;
char ch = Character.toLowerCase(str.charAt(i)); 
str.setCharAt(i, ch);

You are checking the capital letters twice in your loop, you should increment i by 1 when you add in a space. This will also mean you don't need to use i + 1 to de-capitalise the current letter

str.insert(i, ' '); 
i++;
char ch = Character.toLowerCase(str.charAt(i)); 
str.setCharAt(i, ch);

You are checking the capital letters twice in your loop, you should increment i by 1 when you add in a space. This will also mean you don't need to use i + 1 to de-capitalise the current letter

if(Character.isUpperCase(str.charAt(i)))
"andDad"
    ^== (i == 3)

str.insert(i, ' ');
"and Dad"
    ^=== (i == 3)

char ch = Character.toLowerCase(str.charAt(i+1)); 
str.setCharAt((i+1), ch);
"and dad"
     ^=== (i == 3) + 1
i++;
"and dad"
     ^=== (i == 4)

new while loop iteration
"and dad"
     ^=== (i == 4)

change the code in the if this

str.insert(i, ' '); 
i++;
char ch = Character.toLowerCase(str.charAt(i)); 
str.setCharAt(i, ch);
Source Link
Gilsham
  • 153
  • 3

You are checking the capital letters twice in your loop, you should increment i by 1 when you add in a space. This will also mean you don't need to use i + 1 to de-capitalise the current letter

str.insert(i, ' '); 
i++;
char ch = Character.toLowerCase(str.charAt(i)); 
str.setCharAt(i, ch);