-1

How do I loop through a string consisting of numbers and letters and add only numbers to the vector?

For example if the input is:

e385p336J434Y26C2Z6X5Z2

I want to get a vector of int like this:

number = {385, 336, 434, 26, 2, 6, 5, 2}


The best I got was to iterate over the line and add all the digits like that:

#include <bits/stdc++.h>


int main(){
    string f = "e385p336J434Y26C2Z6X5Z2";
    vector<int> f_numb; 
    string sum; 
    for (int i = 0; i < f.size(); ++i){
         if (('0' <= f[i]) && (f[i] <= '9')){
            sum += (f[i]);
            
             
         }
    }
    //std::cout << sum << std::endl; 
    vector<int> m_numb; 
    for (int i = 0; i < sum.size(); ++i){
        m_numb.push_back(sum[i] - '0'); 
    }
    int sm; 
    for (int i = 0; i < m_numb.size(); ++i){
        sm += m_numb[i]; 
        std::cout << m_numb[i] << " "; 
    }
    std::cout << std::endl; 
    
}
cigien
  • 57,834
  • 11
  • 73
  • 112
Swagga
  • 19
  • 1
  • 5
    Just as a side note: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Feb 19 '23 at 11:49
  • 1
    Does your posted code not work? If so, in what way? Please [edit] this question to add this information. Statements such as "it's not working" [are not sufficient descriptions of the problem](https://idownvotedbecau.se/itsnotworking/). – Andreas Wenzel Feb 19 '23 at 11:50
  • Append digits to a string, much like you do now. But add a case when you get a non-digit character, to push the string into your vector and reset the string. – Some programmer dude Feb 19 '23 at 11:50
  • On another note, `int sm;` and then `sm += ...`? Remember that uninitialized variables really are uninitialized, with an *indeterminate* value. Using indeterminate values in any way leads to *undefined behavior*. – Some programmer dude Feb 19 '23 at 11:53

1 Answers1

0

Foregoing the unstated reason you're not using std::isdigit, you can/should simply build each number as you process its digits. Note that the code below does NOT check, nor care, about unsigned overflow, and makes no attempt at processing negative numbers.

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string f = "e385p336J434Y26C2Z6X5Z2";
    std::vector<unsigned int> f_numb;

    for (auto it = f.begin(); it != f.end();)
    {
        if ('0' <= *it && *it <= '9')
        {
            unsigned int sm = 0;
            for (;it != f.end() && '0' <= *it && *it <= '9'; ++it)
                sm = (sm * 10) + (*it - '0');

            f_numb.emplace_back(sm);
        }
        else
        {
            ++it;
        }
    }

    for (auto x : f_numb)
        std::cout << x << ' ';
    std::cout << '\n';
}

Output

385 336 434 26 2 6 5 2 

That, assuming I understand your question vs. your code, which differ highly in their apparent goals.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Thank you so much! this is exactly what I wanted. isdigit I did not use, because I simply did not know about this function:) – Swagga Feb 19 '23 at 14:54