62,614
社区成员
发帖
与我相关
我的任务
分享
String str = "I am a programmer";
String nstr = "";
String substr = "";
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) != ' ') {
substr = str.charAt(i) + substr;
if (i == 0) {
nstr = nstr + substr + " ";
break;
}
} else {
nstr = nstr + substr + " ";
substr = "";
}
}
System.out.println(nstr);
private void button1_Click(object sender, EventArgs e)
{
String st = "I.am..a...programmer";//用.表示 空格
char[] cr = st.ToCharArray();
List<String> tempList = new List<string>(); //可以用其他集合
//其实完全可以用单循环,下边看的是双循环,其实是单循环
for (int i = 0; i < cr.Count(); i++)
{
String tempStr = ""; //如果数据量大不用string保存
for (int j = i; j < cr.Count(); j++)
{
if (cr[j] == '.')
{
tempList.Add(tempStr);
tempList.Add(".");
i = j;
break;
}
tempStr += cr[j];
if (j == cr.Count() - 1)
{
tempList.Add(tempStr);
}
i = j;
}
}
//输出。有很多种方法,这个是没用函数翻转的方法
for (int i = 0; i < tempList.Count(); i++)
{
this.richTextBox1.Text += tempList[tempList.Count() - i-1];
}
}
public class Test{
public static void main(String[] args){
String oldstr="I am a programmer ";
StringBuilder sb= new StringBuilder();
int index = oldstr.lastIndexOf(' ');
while (index >= 0) {
sb.append(oldstr.substring(index + 1) + ' ');
oldstr = oldstr.substring(0,index);
index = oldstr.lastIndexOf(' ');
}
sb.append(oldstr);
System.out.println(sb);
}
}
//方法一
// String str = "I am a programmer";
// String temp[] = str.split(" ");
// String strtemp = "";
// for(int i = temp.length-1 ;i >=0 ;i--){
// strtemp+=temp[i]+" ";
// }
// System.out.print(strtemp);
//方法二
// String oldstr="I am a programmer";
// String[] str = oldstr.split(" ");
// String finalstr="";
// for(int i=str.length-1;i>=0;i--)
// {
// if(str[i].equalsIgnoreCase(""))
// {
// continue;
// }
// if(i==0)
// {
// finalstr=finalstr+str[i].trim();
// continue;
// }
// finalstr=finalstr+str[i].trim()+" ";
// }
// System.out.println(finalstr);
//方法三
// String oldstr = "I am a programmer";
// int index = oldstr.lastIndexOf(' ');
// while (index >= 0) {
// System.out.print(oldstr.substring(index + 1) + ' ');
// oldstr = oldstr.substring(0, index);
// index = oldstr.lastIndexOf(' ');
// }
//
// System.out.println(oldstr);
//方法四
String str = "I love java ddd";
String t[] = str.split("");
String s = "";
for (int i = t.length - 1; i >= 0; i--) {
if (!"".equals(t[i]))
s += t[i];
else
s += " ";
}
System.out.print(s);
package algorithm.common;
import java.util.ArrayList;
import java.util.List;
public class StringReverse
{
/**
*
* @param args
*/
public static void main(String[] args)
{
String s = "I am a programmer";
reverseString(s);
}
/**
* String oldstr="I am a programmer"完全反转为"programmer a am I"
*
* 思路:将char 分为两类,空格和非空格,
*/
public static void reverseString(String str)
{
long a = System.currentTimeMillis();
char[] charArr = str.toCharArray();
List<String> wordList = new ArrayList<String>();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < charArr.length; i++)
{
stringBuffer.append(charArr[i]);
if (isSameKind(i, charArr)) {
continue;
} else {
wordList.add(stringBuffer.toString());
stringBuffer = new StringBuffer();
// stringBuffer.delete(0, stringBuffer.length());
}
}
for (int i = wordList.size()-1;i>=0;i--) {
System.out.print(wordList.get(i));
}
System.out.println();
System.out.println(System.currentTimeMillis() - a);
}
/**
* 判断当前元素i与元素i+1是不是同类
* @param i
* @param charArr
* @return
*/
private static boolean isSameKind(int i, char[] charArr)
{
return i+1 < charArr.length ? (charArr[i] == ' ' ? charArr[i + 1] == ' ' : charArr[i + 1] != ' '):false;
}
}