1

例子:

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

结果:

"               "
"   olleh  z"
"olleh dlrow   "
"x y z "

问题:

反转字符串中的每个世界,忽略空格。

以下是我的代码。基本思想是扫描字符串,当找到一个单词时,然后将其反转。该算法的复杂度为 O(n),其中 n 是字符串的长度。

如何验证它?有更好的解决方案吗?

void reverse_word(char* s, char* e)
{
    while (s < e)
    {
        char tmp = *s;
        *s = *e;
        *e = tmp;
        ++s;
        --e;
    }
}

char* word_start_index(char* p)
{
    while ((*p != '\0') && (*p == ' '))
    {
        ++p;
    }

    if (*p == '\0')
        return NULL;
    else
        return p;
}

char* word_end_index(char* p)
{
    while ((*p != '\0') && (*p != ' '))
    {
        ++p;
    }

    return p-1;
}

void reverse_string(char* s)
{
    char* s_w = NULL;
    char* e_w = NULL;
    char* runner = s;

    while (*runner != '\0')
    {
        char* cur_word_s = word_start_index(runner);
        if (cur_word_s == NULL)
            break;
        char* cur_word_e = word_end_index(cur_word_s);
        reverse_word(cur_word_s, cur_word_e);
        runner = cur_word_e+1;
    }
}
4

5 回答 5

4

您的代码看起来是正确的,但它是纯 C。在 C++ 中,使用相同的方法,它可能看起来像这样:

#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';
}
于 2012-10-14T05:40:01.370 回答
2

就地,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;
}
于 2013-07-21T14:40:18.993 回答
0
#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;
}
于 2013-07-20T08:21:27.100 回答
0
#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;
}
于 2016-08-07T14:02:43.700 回答
-1
#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;
}
于 2018-02-20T08:19:34.993 回答