Created
April 1, 2019 03:53
-
-
Save hlgsdx/65f289e37c5ed556cc21f81ff87d4fbb to your computer and use it in GitHub Desktop.
Revisions
-
SDX created this gist
Apr 1, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // 字符串反转 "You are so pretty." -> "pretty. so are you" #include <stdio.h> #include <stdlib.h> #include <string.h> void strreverse(char *str,size_t nLength){ if(!str) return; for(size_t i=0;i<nLength/2;++i){ if(str[i]==str[nLength-i-1]) continue; str[i]=str[nLength-i-1] ^ str[i]; str[nLength-i-1]=str[nLength-i-1] ^ str[i]; str[i]=str[nLength-i-1] ^ str[i]; } } void strmod(char *str){ if(!str) return; int pos=0; size_t nLength = strlen(str); //先把字符串整体倒过来 strreverse(str,nLength); //再找每个单词倒过来 for(int i=0;i<nLength+1;++i){ if(str[i]==' '||str[i]=='\0'){ strreverse(str+pos,i-pos); pos=i+1; } } } int main(int argc, char *argv[]){ char str[]="You are so pretty."; strmod(str); printf("%s\n",str); return 0; }