Reverse every word in a string (should handle space)

前端 未结 5 526
陌清茗
陌清茗 2020-12-11 12:16

Examples:

char test1[] = \"               \";
char test2[] = \"   hello  z\";
char test3[] = \"hello world   \";
char test4[] = \"x y z \";

5条回答
  •  无人及你
    2020-12-11 13:06

    #include 
    #include
    #include
    using namespace std;
    void Reverse_Each(char st[60])
    { 
        int  i,j,k=0,p=0,l;
        char sr[60]; 
        l=strlen(st);
        for(i=0;i<=l;i++) {
        if((st[i]==' ')||(st[i]=='\0')) {
            for(j=i-1;j>=p;j--) { 
                sr[k]=st[j]; k++;
            }
            sr[k]=' '; k++; p=i+1;}
        }
        for(i=0;i

提交回复
热议问题