0

I need to get a part of a string but 'round' the cut to the next exact char ',' to avoid bad cut ; Much more clear with this example :

string hand =  "AA,KK,QQ,JJ,AKs,AKo,AQs,AQo,TT,AJs,ATs,AJo,KQs,KJs,KTs,QJs,ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,99,A4o,A3s,A2s,KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo,K9s,K8s,K7s,K6s,K5s,K4s,K3s,K2s,Q8s,Q7s,Q6s,Q5s,K9o,J8s,T8s,98s,97s,87s,86s,76s,96s,75s,65s,64s,J9o,T9o,55,54s,53s,52s,K8o,43s,32s,42s,J7s,T7s,K7o,44,33,22,Q4s,Q3s,Q2s,J6s,J5s,T6s,T5s,J4s,K6o,Q8o,J8o,T8o,98o,97o,87o,85s,K5o,K4o,K3o,K2o,95s,74s,76o,65o,54o,84s,94s,Q7o,J7o,T7o,Q6o,J3s,T4s,J2s,Q5o,T3s,T2s,Q4o,J6o,86o,T6o,96o,93s,Q3o,74o,84o,75o,64o,T2o,94o,53o,93o,63o,43o,92o,73o,83o,52o,82o,42o,62o,72o,J5o,63s,92s,73s,Q2o,J4o,83s,85o,82s,T5o,95o,J3o,62s,T4o,J2o,72s,T3o,32o";

The hand lenght is 662 .I want to get lets say the first 7% of the string hand :

  int startpos = 0;
  int stoppos= (662 * 7) / 100;
  string str2 = hand.substr(startpos, stoppos);
  cout <<  str2 << endl;

this will output KTs,QJs,A

As you see the final hand was cut , so i 'd like to substring to the next coma to have this outputt : KTs,QJs,ATo,

i'm a c++ beginer .

  • You've picked the wrong language for string wrangling. If you must, have a look at boost split: http://www.boost.org/doc/libs/1_58_0/doc/html/string_algo/usage.html#idp424404576 – Jonas Byström Jul 18 '15 at 16:05

3 Answers3

0

since each hand takes up 4 characters (including the last comma), you might just round your stoppos to the nearest multiple of 4:

int stoppos = (169 * 7) / 100;
stoppos = stoppos - stoppos % 4; // round down to nearest multiple of 4
stoppos = (4 - stoppos % 4) + stoppos; // round up to nearest multiple of 4

This will output KTs,QJs, or KTs,QJs,ATo,, depending whether you choose to round up or down. If you don't want to keep the last comma, you might do an additional - 1:

stoppos = stoppos - stoppos % 4 - 1;

for more ideas on how to round to the nearest multiple of 4, you can look at this question

Community
  • 1
  • 1
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
0

Use the function from this answer https://stackoverflow.com/a/3407254/2425366

int roundUp(int numToRound, int multiple) { 
    if(multiple == 0) return numToRound;
    int remainder = numToRound % multiple;
    if (remainder == 0) return numToRound;
    return numToRound + multiple - remainder;
}

as stoppos = roundUp(stoppos, 4);

Community
  • 1
  • 1
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

Here is a demonstrative program that shows how it can be done.

#include <iostream>
#include <string>

struct classInstance {};

int main() 
{
    std::string hand = "KTs,QJs,ATo,QTs,JTs,A9s,A9o,KQo,A8s,A8o,A7s,A7o,A6s,A6o,A5s,A5o,A4s,"
                       "99,A4o,A3s,A2s,KJo,J9s,T9s,Q9s,QJo,KTo,Q9o,88,77,66,QTo,A3o,A2o,JTo";

    std::string::size_type start_pos;
    std::string::size_type char_num;

    start_pos = 0;
    char_num = 7 * hand.size() / 100;

    if ( start_pos + char_num < hand.size() )
    {        
        char_num = hand.find( ',', start_pos + char_num );
        if ( char_num != std::string::npos ) char_num -= start_pos - 1; 
    }

    std::string s = hand.substr( start_pos, char_num );

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

The program output is

KTs,QJs,ATo,

Take into account that the second parameter of the member function substr specifies the number of characters to extract.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335