原題:
以單詞為最小單位翻轉(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種的實現(xiàn),比較費空間,因為多申請了一段空間來保存結(jié)果。
在看了其他高手的實現(xiàn)后,發(fā)現(xiàn)可以不用申請空間,并且循環(huán)的次數(shù)更少,也可以實現(xiàn)相同的效果。
大體思路是:
原字符串: the house is blue
先翻轉(zhuǎn)整個字符串-> eulb si esuoh eht
再翻轉(zhuǎn)單個單詞。
代碼:
/********************************************************************
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)整個字符串-> eulb si esuoh eht
再翻轉(zhuǎn)單個單詞。
*********************************************************************/
#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)申請的空間
* src [in] 輸入字符串,需要處理的字符串
* return:
* 處理完成后的 des 指針
* notes:
* 以單詞為最下單位翻轉(zhuǎn)字符串
* 優(yōu)化: 先翻轉(zhuǎn)整個字符串,再翻轉(zhuǎn)單個單詞
*
* 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;
/* 先把整個字符串翻轉(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)單個單詞 */
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)最后一個單詞 */
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
*/
int main()
{
// char src[] = ' all-in-one ';
// char src[] = 'the house is blue';
char src[] = 'zed is dead';
(void) reverse_str_word_by_word2(src);
printf('*****%s*****/n', src);
system('pause');
}
以單詞為最小單位翻轉(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種的實現(xiàn),比較費空間,因為多申請了一段空間來保存結(jié)果。
在看了其他高手的實現(xiàn)后,發(fā)現(xiàn)可以不用申請空間,并且循環(huán)的次數(shù)更少,也可以實現(xiàn)相同的效果。
大體思路是:
原字符串: the house is blue
先翻轉(zhuǎn)整個字符串-> eulb si esuoh eht
再翻轉(zhuǎn)單個單詞。
代碼:
/********************************************************************
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)整個字符串-> eulb si esuoh eht
再翻轉(zhuǎn)單個單詞。
*********************************************************************/
#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)申請的空間
* src [in] 輸入字符串,需要處理的字符串
* return:
* 處理完成后的 des 指針
* notes:
* 以單詞為最下單位翻轉(zhuǎn)字符串
* 優(yōu)化: 先翻轉(zhuǎn)整個字符串,再翻轉(zhuǎn)單個單詞
*
* 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;
/* 先把整個字符串翻轉(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)單個單詞 */
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)最后一個單詞 */
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
*/
int main()
{
// char src[] = ' all-in-one ';
// char src[] = 'the house is blue';
char src[] = 'zed is dead';
(void) reverse_str_word_by_word2(src);
printf('*****%s*****/n', src);
system('pause');
}

