C ++反转字符串但首先打印数字

C++ reverse a string but printing numbers first

我在 class 得到了一个项目并且几乎完成了,我需要接受一串数字和字母以及 return 该字符串,先打印数字,然后打印字母以相反的顺序(例如 abc123 应该 return 123cba)。截至目前,我的代码 returns 是一个字符串,其中数字在前,字母的原始顺序(例如 abc123 returns 123abc)。我可以用两个循环来做到这一点,但是赋值要求我的代码只遍历初始字符串一次。这是我到目前为止的代码...

#include <iostream>
#include <string>
#include "QueType.h"
#include "StackType.h"
using namespace std;

int main ()
{
QueType<char> myQueue;
StackType<char> myStack;
string myString="hello there123";
char curchar;
string numbers, letters;


for (int i = 0; i < myString.length(); i++) {
    if (isdigit(myString.at(i))) {
        myQueue.Enqueue(myString.at(i));
        myQueue.Dequeue(curchar);
        numbers += curchar;
        //cout<<numbers<<endl;
    }
    else if (islower(myString.at(i))) {
        myStack.Push(myString.at(i));
        curchar = myStack.Peek();
        myStack.Pop();
        letters += curchar;
        //cout<<curchar<<endl;
    }
    
}

cout<<(myString = numbers + letters)<<endl;

}

在我的代码中,我有两个设置堆栈和队列的 .h 文件。对于给定的字符串,代码循环遍历字符串以查看它是否看到字母或数字。有了数字,字符串中的点就被保存到队列中,有了字母,它就被保存到堆栈中。

我能想到的唯一另一种颠倒字母顺序的方法是在 if else 语句中,而不是在每个循环中都使用 char = myStack.Peek() ,将其更改为 char += myStack.Peek() 但是当这种情况发生时我会得到奇怪的字母。

因为你已经得到了带字母的字符串,你基本上可以反转它,就是这样。

//emplace version:
void reverse_str(std::string& in)
{
    std::reverse(in.begin(), in.end());
}

//copy version
std::string reverse_str(std::string in)
{
    std::reverse(in.begin(), in.end());
    return in;
}

在您的情况下,emplace 版本将是最佳匹配。 在其他情况下(例如,当您想保留原始字符串时)首选复制版本。


添加示例以使其尽可能简洁。

int main()
{
    std::string inputstr = "123abc";
    std::string numbers{};
    std::string letters{};
    
    
    for(auto c : inputstr)
    {
        if(isdigit(c))
            numbers += c;
        else
            letters += c;
    }
    
    reverse_str(letters); //using the emplace version
    std::cout << numbers + letters;
}

这是我的看法。它只循环一次字符串。我没有你的类型,所以我只使用标准版本。

std::string output;
output.reserve( myString.size() );

std::stack<char> stack;
for ( char c : myString ) {
   if ( std::isdigit( c ) )   // if it's a number, just add it to the output
       output.push_back( c );
   else  // otherwise, add the character to the stack
       stack.push( c );
}

// string is done being processed, so use the stack to get the
// other characters in reverse order
while ( !stack.empty() ) { 
    output.push_back( stack.top() );
    stack.pop();
}

std::cout << output;

工作示例:https://godbolt.org/z/eMazcGsMf

注意:根据您的描述不确定如何处理字母和数字以外的字符,因此将它们与字母一样对待。

一种方法如下:

版本 1

#include <iostream>
#include <string>

int main() {
 std::string s = "abc123";
    
    std::string output;
    output.resize(s.size());
    
   int i = output.length() - 1;
    int j = 0;
    
    for(char &c: s)
    {
        if(!std::isdigit(c))
           {
            
            output.at(i) = c;
            
            --i;
           }
        else
        {
           
           
            output.at(j) = c;
            ++j;
        }
    }
    
    std::cout<<output<<std::endl;
    
    
}

您也可以在上面的程序中使用迭代器来获得想要的结果,如version 2所示。

版本 2

#include <iostream>
#include <string>

int main() {
 std::string s = "abfsc13423";
    
    std::string output;
    output.resize(s.size());
    
   
    std::string::reverse_iterator iter = output.rbegin();
    std::string::iterator begin = output.begin();
   
    
    for(char &c: s)
    {
        if(!std::isdigit(c))
           {
            
            *iter = c;
            
            
            ++iter;
            
           }
        else
        {
            
            *begin = c;
            
             ++begin;
           
        }
    }
    
    std::cout<<output<<std::endl;
    
    
}