Files
Learn-Algorithms/9 Algorithms Job Interview/测试代码/revert_by_word.c
T
2021-03-18 21:24:06 +08:00

58 lines
1002 B
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "stdio.h"
void _reverse(char *start,char *end){
if ((start == NULL) || (end == NULL)) return;
while(start < end){
char tmp = *start;
*start = *end;
*end = tmp;
start++,
end--;
}
}
char *_revert_by_word(char *source){
char *end = source;
char *start = source;
while(*start != '\0'){
if (*start == ' '){
start++;
end++;
}else if(*end == ' ' || *end == '\0'){
_reverse(start,end-1);
start = end;
}else{
end++;
}
}
return source;
}
char *revert_by_word(char *source){
char *end = source;
char *start = source;
if (source == NULL) return NULL;
while (*end != '\0') end++;
end--;
/*整个句子逆序*/
_reverse(start,end);
/*按单词逆序.tneduts a ma I */
_revert_by_word(source);
return source;
}
//为啥运行时 bus error?
int main(int argc, char const *argv[])
{
char *test = "how are you ?";
printf("%s\n",_revert_by_word(test));
//char *test = "I am a student.";
//printf("%s\n",revert_by_word(test));
return 0;
}