The problem with this logic is when i = 12, str[12+1] = ‘t’, so your replace line is really:
str = str.replace('t', 'T');
This will replace only the first occurrence of ‘t’ with ‘T’. The first ‘t’ is in the word ‘Little’, so the first ‘t’ of ‘Little’ gets capitalized to become ‘LiTtle’ and nothing happens to the ‘t’ in ‘tea’.
You can have a similar logic (looking for the space character), but instead of replacing str each time, you need to build a new string one character at a time and use toUpperCase when to replace a single character of the new string.