-3

I've got a std::string number = "55353" and I want to extract the numbers that I've used in this string (5 and 3). Is there a function to do that? If so, please tell me it's name, I've been searching for quite a while now and still haven't found it... UPD: I've solved my problem (kinda)

std::string number(std::to_string(num));

std::string mas = "---------";              

int k = 0;                                  
for (int i = 0; i < number.size(); i++) {
    char check = number[i];                 
    for (int j = 0; j < mas.size(); j++) {
        if (check == mas[j])                
            break;                          
        if (check != mas[j] && check != mas[j+1]) {
            mas[k] = check;                 
            k++;                            
            break;                          
        }                                   
    }                                       
}                                           

mas.resize(k); mas.shrink_to_fit();

std::string mas will contain numbers that were used in std::string number which is a number converted to std::string using std::to_string().

Michael
  • 548
  • 6
  • 23

2 Answers2

0

Try this:

std::string test_data= "55335";
char digit_to_delete = '5';
unsigned int position = test_data.find();
test_data.erase(position, 1);
cout << "The changed string: " << test_data << "\n";

The algorithm is to find the number (as a character) within the string. The position is then used to erase the digit in the string.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Your question looks like homework, so I can guess what you forgot to tell us.

mas starts with ten -. If you spot a 5, you should replace the 6th (!) dash with a '5'. That "6th" is just an artifact of English. C++ starts to count at zero, not one. The position for zero is mas[0], the first element of the array.

The one tricky bit is to understand that characters in a string aren't numbers. The proper term for them is "(decimal) digits". And to get their numerical value, you have to subtract '0' - the character zero. So '5' - '0' == 5 - the character five minus the character zero is the number 5.

MSalters
  • 173,980
  • 10
  • 155
  • 350