反转字符串中的每个单词(应该处理空格)

时间:2012-10-14 04:38:47

标签: c++ c string

示例:

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;
    }
}

5 个答案:

答案 0 :(得分: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';
}

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}

答案 3 :(得分: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;
}

答案 4 :(得分:-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;
}