Reverse every word in a string (should handle space)

前端 未结 5 514
陌清茗
陌清茗 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:01

    In-place, ANSI C89.

    #include <ctype.h>
    #include <stdio.h>
    
    void reverse(char *s) {
        int i = 0, j, k;
        while (1) {
            while (isspace(s[i])) i++;
            if (!s[i]) return;
            for (j = i; !isspace(s[j]) && s[j] != '\0'; j++);
            for (k = 0; k < (j - i) / 2; k++) {
                char t = s[i + k];
                s[i + k] = s[j - k - 1];
                s[j - k - 1] = t;
            }
            i = j;
        }
    }
    
    int main(int argc, char**argv) {
        if (argc != 2) return 1;
        reverse(argv[1]);
        printf("%s\n", argv[1]);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-11 13:05

    Your code seems correct, but it's plain C. In C++, using the same approach, it could look something like this:

    #include <iostream>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <cctype>
    
    int main()
    {
        std::string str = "    cat cow dog wolf     lobster";
        std::string result;
        auto it = str.begin();
    
        while (it != str.end()) {
    
            while (it != str.end() && isspace(*it)) {
                result += *it;
                ++it;
            }
    
            auto begin = it;
            while (it != str.end() && !isspace(*it)) {
                ++it;
            }
            auto end = it;
    
            result.append(std::reverse_iterator<decltype(end)>(end),
                          std::reverse_iterator<decltype(begin)>(begin));
    
            // if you want to modify original string instead, just do this:
            std::reverse(begin, end);
        }
    
        std::cout << result <<'\n';
    }
    
    0 讨论(0)
  • 2020-12-11 13:06
    #include <iostream>
    #include<string.h>
    #include<stdio.h>
    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<p;i++) 
        cout<<sr[i]; 
    } 
    
    int main(){
        char s[60];
        gets(s);
        Reverse_Each(s);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-11 13:13
    #include<iostream>
    #include<string.h>
    
    using namespace std;
    
    int main()
    {
        char s[89];
        cout << "enter\n";
        gets(s);
        int k;
        int p = strlen(s);
        strcat(s," ");
    
        for(int i=0; i <= p; i++)
        {
            if(s[i]==' ')
            {
                for (k = i-1; (k != -1) && (s[k] != ' '); k--)
                    cout<<s[k];
                cout<<" ";
            }
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-11 13:14
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        char str[100],a[10],s[100]=" ";
        int i,j;
        cout<<"enter a string";
        cin.getline(str,100);
        strcat(s,str);
        for(i=0;s[i]!='\0';i++)
        {
            if(s[i]==' '&&s[i+1]!=' '&&s[i+1]!='\0')
            {
                cout<<" ";
                if(i==0)cout<<"\b";
                j=i+1;
                while(s[j]!=' '&&s[j]!='\0')
                {
                    j++;
                }
                j--;
                while(s[j]!=' ')
                {
                    cout<<s[j];
                    j--;
                }
            }
            else if(s[i]==' ')
            {
                cout<<" ";
                if(i==0)cout<<"\b";
            }
        }
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题