http://topic.csdn.net/u/20080711/14/00183de8-5fdf-4fac-a39c-c4dc343b0de5.html?seed=1292595069
将字符串 String oldstr="I am a programmer"完全反转为"programmer a am I",最好不用函数,自己写,当然也可以用
这样的 题目很多人写不出,真是汗啊!我是用链表实现的,但是比较繁琐,算是过关了,今天特来寻求最好的解决方案
public class splitstr {
public static void main(String args[])
{
String oldstr="I am a programmer";
String[] str = oldstr.split(" ");
String finalstr="";
for(int i=str.length-1;i>-1;i--)
{
finalstr=finalstr+str[i]+" ";
}
System.out.println(finalstr);
}
}
--------------------------------------------------------------------------------------------
String oldstr="I am a programmer";
int i= oldstr.length();
char[] tmp=new char[i];
for(int k=0;k tmp[k]=oldstr.charAt(i-k-1);
}
oldstr=String.valueOf(tmp);
System.out.println(oldstr);
--------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test
{
public static void main(String args[])
{
String a = "rosewj";
StringBuffer sb = new StringBuffer();
for(int i = a.length()-1;i>=0;i-- ) {
char c = a.charAt(i);
sb.append(c);
}
System.out.println(sb);
}
}
----------------------------------------------------------------------------------------------------
public class tts {
public static void main(String args[])
{
String oldstr="I am a programmer";
String[] str = oldstr.split(" ");
String finalstr="";
for(int i=str.length-1;i>-1;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 str = "i am a test";
StringBuilder out = new StringBuilder(str.length());
StringBuilder field = new StringBuilder();
char[] charArray = str.toCharArray();
for(int i = charArray.length - 1; i >= 0; i --) {
if(charArray[i] != ' '){
field.insert(0, charArray[i]);
} else {
if(field.length() != 0) {
out.append(field.toString()) ;
field = new StringBuilder();
}
out.append(" ");
}
}
if(field.length() != 0) out.append(field.toString());
System.out.println(out.toString());
---------------------------------------------------------------------------------------------------------------
int i = 0;
String oldstr=" I am a programmer";
int length = oldstr.length(); List<String> list = new ArrayList<String>();
//这是以空格开头的情况
if(oldstr.substring(0, 1).equals(" ")) {
String space = "";
int temp = 0;
for(int m = i; m < length; m++) {
if(oldstr.codePointAt(m) != 32) {
temp = m;
break;
}
}
space = oldstr.substring(i, temp);
list.add(space);
i = temp;
}
while(i < length) {
//获取空格之间的单词
int temp = 0;
Boolean bool = false;
String word = "";
for(int m = i; m < length; m++) {
if(oldstr.codePointAt(m) == 32) {
temp = m;
bool = true;
break;
}
}
if(!bool) {//说明是截取到oldstr的结尾
temp = length;
}
word = oldstr.substring(i, temp);
list.add(word);
i = temp;
bool = false;
//获取单词之间的空格
if(i < length) {
String space = "";
for(int m = i; m < length; m++) {
if(oldstr.codePointAt(m) != 32) {
temp = m;
bool = true;
break;
}
}
if(!bool) {
temp = length;
}
space = oldstr.substring(i, temp);
list.add(space);
i = temp;
}
}
String newstr = "";
for(int j = list.size() - 1; j >= 0; j--) {
newstr += list.get(j);
}
System.out.println(newstr);
--------------------------------------------------------------------------------------------------
public class StringTest {
private static String convert(String str) {
if (str == null) {
return null;
}
char[] cs = str.toCharArray();
char[] cs0 = new char[cs.length];
int temp = cs.length;
for (int start = 0; start < cs.length;) {
boolean isBlank = isBlank(cs[start]);
int end = start;
for (; end < cs.length; end++) {
if (isBlank != isBlank(cs[end])) {
break;
}
}
System.arraycopy(cs, start, cs0, temp - (end - start),
(end - start));
temp -= (end - start);
start = end;
}
return String.valueOf(cs0);
}
private static boolean isBlank(char c) {
return c == ' ';
}
public static void main(String[] args) {
System.out.println(convert("1"));
}
}
---------------------------------------------------------------------------------------------------
public class StringTest {
private static String convert(String str) {
if (str == null) {
return null;
}
char[] cs = str.toCharArray();
char[] cs0 = new char[cs.length];
int temp = cs.length;
for (int start = 0; start < cs.length;) {
boolean isBlank = isBlank(cs[start]);
int end = start;
for (; end < cs.length; end++) {
if (isBlank != isBlank(cs[end])) {
break;
}
}
System.arraycopy(cs, start, cs0, temp - (end - start),
(end - start));
temp -= (end - start);
start = end;
}
return String.valueOf(cs0);
}
private static boolean isBlank(char c) {
return c == ' ';
}
public static void main(String[] args) {
System.out.println(convert("1"));
}
}
-------------------------------------------------------------------------------------------------------
2个栈
先把整个字符串放进第1个栈
然后把第1个栈的字符一个个网外POP 同时PUSH进第2个栈 每遇到一个空格就是一个单词了
这时候把第2个栈的单词POP出来就行了,同时记得加个空格
这样依次把第1个栈内的字符处理完,楼主的要求打到了
-------------------------------------------------------------------------------------------------------
用正则表达式实现
输出结果:
programmer a am I 这里有个空格
------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Arrays;
public class StringFZ {
public static void main(String[] args)
{
String oldstr="I am a programmer";
String[]new1 =oldstr.split(" ",0);
for(int i=new1.length-1;i>=0;i--)
{
System.out.print(new1[i]+" ");
}
}
}
-----------------------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
String str = "I am a programmer";
char[] chs = str.toCharArray();
for(int i = 0, offset = 0; i < chs.length; i++) {
char h = chs[chs.length - 1];
if(h == ' ') {
offset = i;
}
for(int j = chs.length - 2; j >= offset; j--) {
chs[j + 1] = chs[j];
}
if(h == ' ') {
chs[i] = h;
offset++;
} else {
chs[offset] = h;
}
}
System.out.println(chs);
}
}
---------------------------------------------------------------------------------------------------------
public class convertString {
public static void main(String [] args){
char str = ' ';
String oldstr="I am a programmer";
int lenngth = oldstr.length();
StringBuffer newStr = new StringBuffer();
for(int i=0;i < lenngth ;i++){
str = oldstr.charAt(lenngth-1-i);
newStr.append(str);
}
System.out.println(newStr.toString());
}
}
-----------------------------------------------------------------------------------------------------------
public class splitstr {
public static void main(String args[])
{
String oldstr="I am a programmer";
String[] str = oldstr. [color=#0000FF]split(" ",-1);
String finalstr="";
for(int i=str.length-1;i>-1;i--)
{
finalstr=finalstr+str[i]+" ";
}
System.out.println(finalstr);
}
}[/color]
------------------------------------------------------------------------------------------------------------
public class splitstr {
public static void main(String args[])
{
String oldstr="I am a programmer";
String[] str = oldstr.split(" ");
String finalstr="";
for(int i=str.length-1;i>-1;i--)
{
finalstr=finalstr+str[i]+" ";
}
System.out.println(finalstr);
}
}
------------------------------------------------------------------------------------------------------------
public class splitstr {
public static void main(String args[])
{
String oldstr="I am a programmer";
String[] str = oldstr.split(" ",-1);
String finalstr="";
for(int i=str.length-1;i>-1;i--)
{
finalstr=finalstr+str[i]+" ";
}
System.out.println(finalstr);
}
}
-------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.Stack;
public class Test1 {
public static void main(String[] args) {
String oldstr="I am a programmer";
Stack<String> newStr = new Stack<String>();
String temp = "";
for (int i = 0; i < oldstr.length(); i++) {
if (i != oldstr.length() - 1 && oldstr.charAt(i) != ' ')
temp += oldstr.charAt(i);
else {
newStr.add(temp);
temp = "";
}
}
while(!newStr.empty())
System.out.print(newStr.pop() + " ");
}
}
------------------------------------------------------------------------------------------------------------
Java codepublic class splitstr {
public static void main(String[] args) {
String s = "i am a programmer";
String regu = "//s+";
String regu1 = "//w+";
String[] eg = s.split(regu);
String[] eg1 = s.split(regu1);
String result = "";
for(int i=eg.length-1;i>=0;i--){
result += eg[i]+eg1[i];
}
System.out.println(result);
}
}
---------------------------------------------------------------------------------------------------------------
public static void main(String[] args) {
String s = "I am a Programmer";
int[] a = new int[100];
int i = 0, j = 1;
boolean flag = true;
a[0] = 0;
for(;i<s.length(); i++) {
if((s.charAt(i)==' ')==flag) {
a[j++] = i;
flag = !flag;
} } a[j--] = s.length();
String result = "";
while(j>=0) {
result += s.substring(a[j], a[j+1]);
j--;
}
System.out.println(result);
}
----------------------------------------------------------------------------------------------------------------
我好奇用c++写了一个 也是可以的。。
void changeDown(char const *str)
{
char strtemp[255];
int count=0,j=0;
while(str[j++])count++;
j=0;
for (int i=count-1;i>=0;i--)
{
if (str[i]!=' ')
{
if (i==0)
{
strtemp[j++]=str[i];
j--;
while (j!=-1)
{
cout <
cout < <" ";
}
else
{
strtemp[j++]=str[i];
}
}
else//==' '
{
j--;
while (j!=-1)
{
cout <
cout < <" ";
j=0;
}
}
}
---------------------------------------------------------------------------------------------------------------
public static void main(String []args)
南开一百题里面的啊,比较简单的一个题目。
---------------------------------------------------------------------------------------------------------------------------------
我用堆栈写了一个
-------------------------------------------------------------------------
import java.util.*; public class UsingStackReverseString { public static void main (String args[]){ String str1 = " I am a programmer "; String str2 = ""; Stack stack1 = new Stack(); Stack stack2 = new Stack(); char[] strArray = str1.toCharArray(); for (int i=0; i<strArray.length; i++){ stack1.push(strArray[i]); } while (!stack1.empty()){ if (stack1.peek() == (Object)' '){ while (!stack2.empty()){ str2 += stack2.pop(); } str2 += stack1.pop(); } else{ stack2.push(stack1.pop()); } while (!stack2.empty()){ str2 += stack2.pop(); } } System.out.print(str2); } }
-------------------------------------------------------------------------
我给出一个代码,大家参考
-------------------------------------------------------------------------------C/C++ code
---------------------------------------------------------------------