java字母反过来_java之字母反转~~ - Plight - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
字符串的反轉(zhuǎn)輸出
這也是面試題中常考的一道。我們就以一個(gè)包含了全部26個(gè)英文字母,同時(shí)又具有完整含義的最短句子作為例子來(lái)完成解答。先來(lái)看一下這個(gè)句子:
引用
A quick brown fox jumps over the lazy dog.(一只輕巧的棕色狐貍從那條懶狗身上跳了過(guò)去。)
最常用的方式就是反向取出每個(gè)位置的字符,然后依次將它們輸出到控制臺(tái):
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("反轉(zhuǎn)后字符串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
}
// 也可以轉(zhuǎn)換成數(shù)組后再反轉(zhuǎn),不過(guò)有點(diǎn)多此一舉
char[] data = s.toCharArray();
System.out.println();
System.out.print("反轉(zhuǎn)后字符串:");
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("反轉(zhuǎn)后字符串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
// 也可以轉(zhuǎn)換成數(shù)組后再反轉(zhuǎn),不過(guò)有點(diǎn)多此一舉
char[] data = s.toCharArray();
System.out.println();
System.out.print("反轉(zhuǎn)后字符串:");
for (int i = data.length; i > 0; i--) {
System.out.print(data[i - 1]);
}
}
}
運(yùn)行結(jié)果:
1、原始的字符串:A quick brown fox jumps over the lazy dog.
2、反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
3、反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
以上兩種方式雖然常用,但卻不是最簡(jiǎn)單的方式,更簡(jiǎn)單的是使用現(xiàn)有的方法:
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("反轉(zhuǎn)后字符串:");
StringBuffer buff = new StringBuffer(s);
// java.lang.StringBuffer類(lèi)的reverse()方法可以將字符串反轉(zhuǎn)
System.out.println(buff.reverse().toString());
}
}
運(yùn)行結(jié)果:
1、原始的字符串:A quick brown fox jumps over the lazy dog.
2、反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
編輯特別推薦:
a
原題:
以單詞為最小單位翻轉(zhuǎn)字符串
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種的實(shí)現(xiàn),比較費(fèi)空間,因?yàn)槎嗌暾?qǐng)了一段空間來(lái)保存結(jié)果。
在看了其他高手的實(shí)現(xiàn)后,發(fā)現(xiàn)可以不用申請(qǐng)空間,并且循環(huán)的次數(shù)更少,也可以實(shí)現(xiàn)相同的效果。
大體思路是:
原字符串: The house is blue
先翻轉(zhuǎn)整個(gè)字符串-> eulb si esuoh ehT
再翻轉(zhuǎn)單個(gè)單詞。
代碼:
/********************************************************************
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: 以單詞為最小單位翻轉(zhuǎn)字符串-2 優(yōu)化版
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"
參考其他高手的思路:
先翻轉(zhuǎn)整個(gè)字符串-> eulb si esuoh ehT
再翻轉(zhuǎn)單個(gè)單詞。
*********************************************************************/
#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 指向?qū)崿F(xiàn)申請(qǐng)的空間
* src [in] 輸入字符串,需要處理的字符串
* return:
* 處理完成后的 des 指針
* notes:
* 以單詞為最下單位翻轉(zhuǎn)字符串
* 優(yōu)化: 先翻轉(zhuǎn)整個(gè)字符串,再翻轉(zhuǎn)單個(gè)單詞
*
* 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;
/* 先把整個(gè)字符串翻轉(zhuǎn)一次 */
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
/* 再翻轉(zhuǎn)單個(gè)單詞 */
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++;
}
}
/* 翻轉(zhuǎn)最后一個(gè)單詞 */
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
總結(jié)
以上是生活随笔為你收集整理的java字母反过来_java之字母反转~~ - Plight - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 不含抽象方法的抽象类 java_Java
- 下一篇: android java和c混合编程_C