Extra bracket in if condition. It should be:
if(S.charAt(i)==' ')