解决方案 »
- JTextArea文本內容行数的一个奇怪问题
- 关于swingX 中的ColumnControlButton实现Jtable的列隐藏和显示的问题?
- 求人鸡狗米问题(编程)
- 问一个目录问题?
- 在jsp中加载applet,显示找不到class的错误?
- 如何在jvm中删除classloader动态载入的class?
- 在调试中出现 "row index out of change"?怎么解决?求助!
- IE不能显示用了swing 的APPLET??? 在线等~~~~~~~~`
- 如果把一个字符串按指定方式解码?
- 一个关于软件分发的问题
- 【Java 千古奇BUG】各路牛人大神过来围观!帮忙看看
- 匿名内部类错误,抄书的,肯定是小错误,大神们看看,指教下!!!
1、原句中哪里有空格,输出就哪里有空格,并且原句几个输出就几个
2、每个单词长度短(短指在char范围内,不会有长度几万的单词)
StringBuilder sentence = new StringBuilder("This is an apple");
char c;
int p1, p2;
p1 = sentence.length();
while (p1 > 0) {
p1--;
p2 = p1;
while (p1 >= 0 && sentence.charAt(p1) != ' ') {
p1--;
}
c = (char) (p2 - p1);
p2 = p1 + 1;
while (p1 + c >= p2) {
System.out.print(sentence.charAt(p2));
p2++;
}
if (p1 >= 0) {
System.out.print(' ');
}
}
public static void main(String[] args) {
StringBuilder sentence = new StringBuilder("This is an apple"); int p1 = sentence.length() - 1;
int p2 = 1;
char c; while (p1 >= 0) {
c = sentence.charAt(p1);
while (c != ' ' && p1 > 0) {
c = sentence.charAt(--p1);
}
System.out.print(c);
c = sentence.charAt(++p1); while (c != ' ' && p1 < sentence.length()) {
c = sentence.charAt(p1++);
System.out.print(c);
++p2;
} p1 = p1 - p2 - 1;
p2 = 1;
}
}
}
static char c = ' ';
static StringBuilder sentence = new StringBuilder("This is an apple");
static int p1 = sentence.length();
static int p2 = 0;
public static void main(String[] args) {
do{
p1 = getIndex(sentence ,p1,p2,c);
} while( p1 != -10 );
}
public static int getIndex(StringBuilder sentence ,int p1,int p2,char c){
if( sentence.substring(0, p1).lastIndexOf(c) > 0 ){
p2 = sentence.substring(0, p1).lastIndexOf(c);
System.out.print(sentence.substring(0, p1).substring(p2+1)+c);
}else{
System.out.print(sentence.substring(0, p1));
p2 = -10;
}
return p2;
}
}
char c;int p1;int p2;
System.out.println(sentence.toString().split("\\s+")[3]+" "+sentence.toString().split("\\s")[2]+" "
+ sentence.toString().split("\\s+")[1]+" "+sentence.toString().split("\\s")[0]);
如果是这样的话,可以采用两次置换的方法,先将整个字符串颠倒,结果是
elppa na si sihT
然后再对每一个单词进行颠倒,结果是
apple na is This
int p1=0,p2;
StringBuilder sentence = new StringBuilder("This is an apple");
p2=sentence.toString().length();
while((p1=sentence.toString().lastIndexOf(" ",p2-1))!=-1){
System.out.print(sentence.toString().substring(p1+1,p2)+c);
p2=sentence.toString().lastIndexOf(" ",p1);
if(sentence.toString().lastIndexOf(" ",p2-1)==-1){
System.out.print(sentence.toString().substring(0,p2));
}
}
while(true){
p1 = 0;
for(int j=0;j<p2;j++){
c = sentence.charAt(j);
if(c ==' '){
p1 = j;
}
}
if(p1!=0){
System.out.print(sentence.substring(p1+1, p2)+" ");
}else{
System.out.print(sentence.substring(0, p2));
break;
}
p2 = p1;
}
int p1, p2; p1 = p2 = sentence.length() - 1; while (p1 >= 0) {
c = sentence.charAt(p1);
if (c == ' ' || p1 == 0) { if (p1 == 0) {
System.out.print(sentence.charAt(p1));
} c = (char) p1;
while (p1 < p2) {
System.out.print(sentence.charAt(p1 + 1));
p1++;
} p1 = c;
p2 = p1 - 1; if (p1 > 0) {
System.out.print(sentence.charAt(p1));
} }
p1--;
}
只要两个int就行了。 StringBuilder sentence = new StringBuilder("This is an apple");
int p1, p2;
p1 = p2 = sentence.length();
while (p1 >= 0) {
if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
System.out.print(sentence.substring(p1, p2));
if (p1 >= 1)
System.out.print(' ');
p2 = p1 - 1;
}
p1--;
}
}否则 StringBuilder sentence = new StringBuilder("This is an apple");
int p1, p2;
char c;
p1 = p2 = sentence.length();
while (p1 >= 0) {
if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
c = (char) p1;
while (p1 < p2) {
System.out.print(sentence.charAt(p1++));
}
p1 = c;
if (p1 >= 1)
System.out.print(' ');
p2 = p1 - 1;
}
p1--;
}
发现3楼自己写的代码,有个小bug,所以重新发个
StringBuffer str = new StringBuffer("This is an apple"); int p1 = str.length() - 1;
int p2;
char c; while (p1 >= 0) {
c = str.charAt(p1); while (c != ' ' && p1 > 0) {
c = str.charAt(--p1);
} if (c != ' ') {
System.out.print(' ');
} System.out.print(c); p2 = p1;
c = str.charAt(++p2); while (c != ' ' && p2 < str.length()) {
c = str.charAt(p2++); if (c != ' ') {
System.out.print(c);
}
} p1--;
}
这个题如果有C有基础, 算是入门题