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

Error when trying to reverse string except for special characters

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

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] =='[=11=]') {
            break;
        }
    }

    //cout<<len<<endl;

    t[len] = '[=11=]';
    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 作为输出。我犯了什么错误?

我突然想到用 boost::iterator::filter_iterator and std::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

请注意,这仅将逗号视为特殊逗号,但您明白了。


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

#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--] = '[=12=]';

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

使用标准 class 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!$