反转字符串中每一个字(应处理空间) [英] Reverse every word in a string (should handle space)

查看:106
本文介绍了反转字符串中每一个字(应处理空间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

 字符TEST1 [] =;
CHAR TEST2 [] =你好Z;
CHAR TEST3 [] =Hello World的;
CHAR TEST4 [] =X Y Z;

结果:

 
东海生日贺Z
东海生日贺dlrow
X Y Z

问题:


  

反向每一个世界在一个字符串,忽略空格。


  
  

以下是我的code。其基本思路是扫描字符串时,
  查找单词,然后扭转它。该算法的复杂度是
  为O(n),其中n是串的长度。


  
  

如何验证?有没有更好的解决办法?


 无效reverse_word(字符* S,字符* E)
{
    而(S< E)
    {
        焦炭TMP = * S;
        * S = * E;
        * E = tmp目录;
        ++ S;
        --e;
    }
}字符* word_start_index(字符* P)
{
    而((* p ='\\ 0')及!及(* P ==''))
    {
        ++磷;
    }    如果(* P =='\\ 0')
        返回NULL;
    其他
        回磷;
}字符* word_end_index(字符* P)
{
    而((* p ='\\ 0')及!&安培;!(* p =''))
    {
        ++磷;
    }    返回对 - 1;
}无效reverse_string(字符* S)
{
    字符* s_w = NULL;
    字符* e_w = NULL;
    字符*亚军= S;    而(*亚军!='\\ 0')
    {
        字符* cur_word_s = word_start_index(亚军);
        如果(cur_word_s == NULL)
            打破;
        字符* cur_word_e = word_end_index(cur_word_s);
        reverse_word(cur_word_s,cur_word_e);
        转轮= cur_word_e + 1;
    }
}


解决方案

您code似乎是正确的,但它是普通的C.在C ++中,用同样的方法,它可能是这个样子:

 的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;&迭代器GT;
#包括LT&;&算法GT;
#包括LT&;&cctype GT;诠释的main()
{
    性病::字符串str =猫牛狗狼龙虾;
    性病::字符串结果;
    汽车IT = str.begin();    而(它!= str.end()){        而(它= str.end()及!&安培; isspace为(*吧)){
            结果+ = *它;
            ++它;
        }        自动开始=它;
        而(它= str.end()及!&安培;!isspace为(*吧)){
            ++它;
        }
        自动结束=它;        result.append(的std :: reverse_iterator的< decltype(完)>(结束)
                      的std :: reverse_iterator的< decltype(开始)>(开始));        //如果要修改原来的字符串,而不是,只是这样做:
        性病::扭转(开始,结束);
    }    性病::法院LT&;<结果<<'\\ n';
}

Examples:

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

Results:

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

The problem:

Reverse every world in a string, ignore the spaces.

The following is my code. The basic idea is to scan the string, when finding a word, then reverse it. The complexity of the algorithm is O(n), where n is the length of the string.

How do verify it? Is there a better solution?

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

解决方案

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

这篇关于反转字符串中每一个字(应处理空间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆