i have to cut
string into equal length peaces according to
the length of the first
word, and delete spaces between the words to one space like this
( _ = space )
"_ _ _ How__ do__ you__ do__today "
now it returns
How
__d
o__
you
__d
o__
tod
ay_
should be
How
do_
you
do_
tod
ay_
the following code cuts the string into equal length
peaces but it doesn�t cut the spaces between words so
that there is only one space that between the words.
How can i do that???
import java.io.*;
class Cutting{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String str=" How do you do today ";
String[] cutStr;
char spr= (' ');
char rpl= (' ');
int index=0;
int offset =0;
cutStr=cut(str,spr,rpl,stdin);
for(index=0;index<str.length();index++)>
System.out.println( cutStr[index]);
//System.out.print("\n" +(cutStr=cut(str, spr, rpl))+"totanoi");
}//main
static String[] cut(String str, char spr, char rpl,BufferedReader stdin)throws IOException {
str = str.trim();
int wordLen = str.indexOf(spr);
if (wordLen == -1) {
return new String[]{ str};
}
int words = (int)Math.ceil(str.length() / (double)wordLen);
String[] cutStr = new String[words];
int offset = 0, index = 0;
for (; (offset + wordLen) < str.length(); ++index) {
cutStr[index] = str.substring(offset, (offset + wordLen)).replace(spr, rpl);
offset += wordLen;
}
cutStr[index] = str.substring(offset, str.length()).replace(spr, rpl);
while (cutStr[index].length() < wordLen) {
cutStr[index] += rpl;
}
return cutStr;
}
}//class