尝试反转字符串时出错,但特殊字符除外

时间:2016-03-06 13:59:44

标签: c++ string algorithm reverse

我只需要反转字符串中的字符串部分,同时保持字符串中的特殊字符不变。例如,

Input:   str = "Ab,c,de!$"
Output:  str = "ed,c,bA!$"

我编写了以下C ++代码来做同样的事情:

#include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
using namespace std;


int main() {
    char s[100],t[100];
    cin>>s;

    int len;
    for ( len = 0; ; len++ ) {
        if( s[len] =='\0') {
            break;
        }
    }

    //cout<<len<<endl;

    t[len] = '\0';
    for ( int i = 0; i < len; i++ ) {
        if ( isalpha(s[i])) {
            t[len-i-1] = s[i];
           // cout<<t[len-i-1]<<endl;
        }
        else {
            t[i] = s[i];
            //cout<<t[i]<<endl;
        }
    }
    //cout<<s<<endl;
    cout<<t;
    return 0;
}

对于上面提到的示例输入,我将SOH作为输出。我犯的错是什么?

2 个答案:

答案 0 :(得分:3)

我突然想到使用boost::iterator::filter_iteratorstd::reverse来写这个是很自然的:

#include <string>
#include <algorithm>
#include <iostream>

#include <boost/iterator/filter_iterator.hpp>

using namespace std;

int main()
{
    auto is_regular = [](char c){return c != ',';};
    string s{"hello, cruel world"};                                                                                                         
    std::reverse(
        boost::make_filter_iterator(is_regular, s.begin(), s.end()),
        boost::make_filter_iterator(is_regular, s.end(), s.end()));
    cout << s << endl;
}   

输出:

dlrow, leurc olleh

请注意,这只会将逗号视为特殊符号,但您会明白这一点。

以下是一个解决方案,更符合您问题中的代码。我认为问题在于,要正确地执行此操作,您需要两个索引 - 一个从左侧运行,一个从右侧运行 - 并运行直到它们相互交叉。否则你可以得到各种奇怪的东西,比如两次交叉的东西。

#include<stdio.h>                                                                                                                                                                                            
#include<iostream>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<cstring>


using namespace std;


int main()
{
    char s[101]="hello, cruel world", t[100];

    auto len = strlen(s);
    t[len--] = '\0';

    //cout<<len<<endl;

    int i = 0;
    while(i <= len)
        if (!isalpha(s[i]))
        {
            t[i] = s[i];
            ++i;
            continue;
        }
        else if (!isalpha(s[len]))
        {
            t[len] = s[len];
            --len;
            continue;
        }
        else
        {
            t[len] = s[i];
            t[i] = s[len];
            ++i;
            --len;
        }

    cout<<t<< endl;
    return 0;
}

答案 1 :(得分:2)

使用标准类std::string而不是字符数组更好更安全。

程序可以按以下方式查看

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string s;

    std::getline( std::cin, s );

    if ( !s.empty() )
    {        
        for ( std::string::size_type first = 0, last = s.size(); first < --last; ++first )
        {
            while ( first != last && !std::isalpha( ( unsigned char )s[first] ) ) ++first;
            while ( first != last && !std::isalpha( ( unsigned char )s[last] ) ) --last;
            if ( first != last ) std::swap( s[first], s[last] );
        }

        std::cout << s << std::endl;
    }        
}    

如果要输入字符串

Ab,c,de!$

然后输出看起来像

Ab,c,de!$
ed,c,bA!$