String to Integer array | Sololearn: Learn to code for FREE!


String to Integer array

Hey I needed some help. How do I convert the string into an integer array in C++ ? I'm taking the string from command line . Edit : After conversion I'm facing problem with the array storage. I've used a for(i=0;str.length();i++) to display the integer array since arr where the array is stored won't have length / size function.


8/22/2019 3:20:51 PM

Sreyashi Bhattacharjee

4 Answers

New Answer


atoi() convert a char array to int int x=0; this is how to convert it manually string str="543; for(int i=0;i<str.length();i++) { x*=10; x+=str[i]-'0'; }


Sreyashi Bhattacharjee char '0' has a ascii value of 48, to get a an integer 0 you need to subtract 48 from each char value. I am also assuming you have number string at hand. Rest you should know what to do.


~ swim ~ yes it's a number string in the form of '1 2 3 4' . Thank you .