Skip to content

Instantly share code, notes, and snippets.

@hlgsdx
Created April 1, 2019 03:53
Show Gist options
  • Select an option

  • Save hlgsdx/65f289e37c5ed556cc21f81ff87d4fbb to your computer and use it in GitHub Desktop.

Select an option

Save hlgsdx/65f289e37c5ed556cc21f81ff87d4fbb to your computer and use it in GitHub Desktop.

Revisions

  1. SDX created this gist Apr 1, 2019.
    35 changes: 35 additions & 0 deletions main.c
    Original 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;
    }