How do I turn a string number into an int array of the individual digits

General Tech Bugs & Fixes 2 years ago

3 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 30 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I have 20 digit strings ex: 12345678912345678912. I want to turn this into an array of ints [1,2,3...2] How would I do that? (I kept getting errors with sstream, atoi/stoi)

profilepic.png
manpreet 2 years ago

Create a new array, and convert each number character to number. Just subtract '0' from the number character and you will get the number.

The number character - '0' = the ASCII value of that character - the ASCII value of '0' = the number.

std::vector<int> digits;
for (int i = 0; i < s.size(); i++)
    digits.push_back(s[i] - '0');

3 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.