Re: [問題] 反轉字串裡面的字元
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)
討論串 (同標題文章)