简体   繁体   中英

how to use substr get string from right hand side in c++

I have a string of numbers for example 469111252 and I know how to split from left-hand side but how to use substr to split it from right-hand side?

If digitsPerNode = 2

from left 46 91 11 25 2

But I want to get from right 52 12 11 69 4

//left hand side parse
for (int i=0;i<num.length(); i+=digitsPerNode) {
    splitNum = num.substr(i,digitsPerNode);
}

Count from top to bottom not from bottom to top.

int numlength=num.length();

if(numlength%2==1)
numlength+=1;

    for(int i=numlength;i>=0;i-=digitsPerNode)
    {
        splitNum = num.substr(i,digitsPerNode);
    {
string a = "1234567890-=4";
for (int i = a.length() - 2; i >= -1; i = i - 2)
{
    if (i < 0) cout << a.substr(i+1, 1) << endl;
    else cout << a.substr(i, 2) << endl;
}

I found the solution with reverse function

DoublyLinkedList::DoublyLinkedList(string value, string dPerNode)
{
    digitsPerNode = stoi(dPerNode);
    string subString;
    reverse(value.begin(), value.end());
    for (int i = 0; i <= value.length(); i += digitsPerNode)
    {
        //cut string by digitsPerNode Ex: 3 500 123 412
        subString = value.substr(i, digitsPerNode);
        reverse(subString.begin(), subString.end());
        insertAtHead(subString);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM