Re: [問題] 反轉字串裡面的字元

看板C_and_CPP作者 (張昱珩)時間13年前 (2012/10/25 10:40), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/7 (看更多)
http://ideone.com/BP6IuE #include <stdio.h> #include <string.h> void reverse_char_array(char * first, char * last) { while (first != last && first != --last) { *first ^= *last; *last ^= *first; *first++ ^= *last; } } void reverse_string(char * sentence) { reverse_char_array(sentence, &sentence[strlen(sentence)]); } void reverse_word(char * sentence) { char * end_s = &sentence[strlen(sentence)], * end_w = NULL; while (end_w != end_s) { if (!(end_w = strchr(sentence, ' '))) end_w = end_s; reverse_char_array(sentence, end_w); sentence = end_w + 1; } } int main(int argc, char ** argv) { char sentence[] = "how are you"; reverse_string(sentence); reverse_word(sentence); printf("%s\n", sentence); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 42.66.191.129 ※ 編輯: changyuheng 來自: 36.224.171.163 (10/27 00:00)
文章代碼(AID): #1GYAQ2Yu (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1GYAQ2Yu (C_and_CPP)