( ) return y.replaceAll("\\s+"," "); ( )
and StringBuffer.length()- constant time (without slow semantics of output completion in java)
and similarly x.charAt(x.length());also produces StringIndexOutOfBoundsException(and does not return \0, as you would expect in C)
for fixed code:
while ( y.length()>i)
{
if (y.charAt(i) != ' ')
{
y.setCharAt(j, y.charAt(i));
i++;
j++;
}
else
{
y.setCharAt(j, y.charAt(i));
i++;
j++;
while (y.charAt(i) == ' ')
i++;
}
}
y.setLength(j);
btw a StringBuilder is a faster implementation (without too much synchronization)
source
share