int i = 0;
StringBuffer str = new StringBuffer();
while(k > 0) {
char ch = s.charAt(i);
if(ch == ' '|| i == s.length()-1){
k--;
}
if(k != 0 ||i == s.length()-1&&ch!=' ')
str.append(ch);
i++;
}
return str.toString();