字符串的反转输出

这也是面试题中常考的一道。我们就以一个包含了全部26个英文字母,同时又具有完整含义的最短句子作为例子来完成解答。先来看一下这个句子:

引用

A quick brown fox jumps over the lazy dog.(一只轻巧的棕色狐狸从那条懒狗身上跳了过去。)

最常用的方式就是反向取出每个位置的字符,然后依次将它们输出到控制台:

public class StringReverse {
public static void main(String[] args) {
// 原始字符串
String s = "A quick brown fox jumps over the lazy dog.";
System.out.println("原始的字符串:" + s);
System.out.print("反转后字符串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
}
// 也可以转换成数组后再反转,不过有点多此一举
char[] data = s.toCharArray();
System.out.println();
System.out.print("反转后字符串:");
for (int i = data.length; i > 0; i--) {
System.out.print(data[i - 1]);
}
}
}
public class StringReverse {
public static void main(String[] args) {
// 原始字符串
String s = "A quick brown fox jumps over the lazy dog.";
System.out.println("原始的字符串:" + s);
System.out.print("反转后字符串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
// 也可以转换成数组后再反转,不过有点多此一举
char[] data = s.toCharArray();
System.out.println();
System.out.print("反转后字符串:");
for (int i = data.length; i > 0; i--) {
System.out.print(data[i - 1]);
}
}
}

运行结果:

1、原始的字符串:A quick brown fox jumps over the lazy dog.

2、反转后字符串:.god yzal eht revo spmuj xof nworb kciuq A

3、反转后字符串:.god yzal eht revo spmuj xof nworb kciuq A

以上两种方式虽然常用,但却不是最简单的方式,更简单的是使用现有的方法:

Java代码

public class StringReverse {
public static void main(String[] args) {
// 原始字符串
String s = "A quick brown fox jumps over the lazy dog.";
System.out.println("原始的字符串:" + s);
System.out.print("反转后字符串:");
StringBuffer buff = new StringBuffer(s);
// java.lang.StringBuffer类的reverse()方法可以将字符串反转
System.out.println(buff.reverse().toString());
}
}

运行结果:

1、原始的字符串:A quick brown fox jumps over the lazy dog.

2、反转后字符串:.god yzal eht revo spmuj xof nworb kciuq A

编辑特别推荐:

a

原题:

以单词为最小单位翻转字符串

Write the function String reverseStringWordByWord(String input) that reverses

a string word by word. For instance,

reverseStringWordByWord("The house is blue") --> "blue is house The"

reverseStringWordByWord("Zed is dead") --> "dead is Zed"

reverseStringWordByWord("All-in-one") --> "All-in-one"

面试系列4种的实现,比较费空间,因为多申请了一段空间来保存结果。

在看了其他高手的实现后,发现可以不用申请空间,并且循环的次数更少,也可以实现相同的效果。

大体思路是:

原字符串: The house is blue

先翻转整个字符串-> eulb si esuoh ehT

再翻转单个单词。

代码:

/********************************************************************
created: 2006/06/16
filename: C:\Documents and Settings\Administrator\桌面\flwo\reverse2.c
file path: C:\Documents and Settings\Administrator\桌面\flwo
file base: reverse
file ext: c
author: A.TNG
version: 0.0.1
purpose: 以单词为最小单位翻转字符串-2 优化版
Write the function String reverseStringWordByWord(String input)
that reverses a string word by word. For instance,
reverseStringWordByWord("The house is blue") --> "blue is house The"
reverseStringWordByWord("Zed is dead") --> "dead is Zed"
reverseStringWordByWord("All-in-one") --> "All-in-one"

参考其他高手的思路:

先翻转整个字符串-> eulb si esuoh ehT

再翻转单个单词。

*********************************************************************/
#include 
#include 
#include 
#define REVERSE_WORD(p1, p2) while (p1 <= p2) { char ch; ch = *p1; *p1 = *p2; *p2 = ch; p1++; p2--; }
/*
* name: reverse_src_word_by_word
* params:
* des [out] 输出字符串, des 指向实现申请的空间
* src [in] 输入字符串,需要处理的字符串
* return:
* 处理完成后的 des 指针
* notes:
* 以单词为最下单位翻转字符串
* 优化: 先翻转整个字符串,再翻转单个单词
*
* author: A.TNG 2006/06/16 10:37
*/
char * reverse_str_word_by_word2(char *src)
{
char *p1, *p2;
int n_src_len;
if (NULL == src)
return NULL;
/* 先把整个字符串翻转一次 */
n_src_len = strlen(src);
p1 = src; p2 = src + n_src_len - 1;
REVERSE_WORD(p1, p2);
#if 0
while (p1 <= p2)
{
/* 交换头字符和尾字符 */
char ch;
ch = *p1; *p1 = *p2; *p2 = ch;
p1++; p2--;
}
#endif
/* 再翻转单个单词 */
p1 = src; p2 = p1;
while ('\0' != *p2)
{
if (' ' == *p2)
{
char *p;
p = p2 - 1;
REVERSE_WORD(p1, p);
#if 0
while (p1 <= p)
{
char ch;
ch = *p1; *p1 = *p; *p = ch;
p1++; p--;
}
#endif
p2++;
p1 = p2;
}
else
{
p2++;
}
}
/* 翻转最后一个单词 */
p2--;
REVERSE_WORD(p1, p2);
#if 0
while (p1 <= p2)
{
char ch;
ch = *p1; *p1 = *p2; *p2 = ch;
p1++; p2--;
}
#endif
return src;
}
/*
* name: main
* params:
* none
* return:
* none
* notes:
* none
*
* author: A.TNG 2006/06/16 10:37