How can i convert string into int array in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can i convert string into int array in c++?

2nd Jan 2021, 6:19 PM
R SF
R SF - avatar
18 Answers
+ 3
What do you want to store in the int array ?
2nd Jan 2021, 7:00 PM
sneeze
sneeze - avatar
+ 2
Thank u for answering my question🙏
2nd Jan 2021, 7:32 PM
R SF
R SF - avatar
+ 1
I want to get a string of numbers from the user and store it in the array and then implement the QuickSort function on it
2nd Jan 2021, 7:03 PM
R SF
R SF - avatar
0
What is the language you want to use. please add the language you use in the tags.
2nd Jan 2021, 6:27 PM
sneeze
sneeze - avatar
0
C++
2nd Jan 2021, 6:37 PM
R SF
R SF - avatar
0
How are the numbers separated ?
2nd Jan 2021, 7:05 PM
sneeze
sneeze - avatar
0
With spaces
2nd Jan 2021, 7:06 PM
R SF
R SF - avatar
0
Do u have any idea ?
2nd Jan 2021, 7:23 PM
R SF
R SF - avatar
0
There is no easy, ready made function in c++ to do this. Use a while loop (till end of string) Read string by character. if it is a character, add it to a temp string if it is another character, add it to the temp string if it is a space, add the temp string to a string array Loop throug string array and convert string to int. Sorry do not know c++ that well.
2nd Jan 2021, 7:29 PM
sneeze
sneeze - avatar
0
Give example input and expected output. Is you string a C++ string or a c-style character array?
2nd Jan 2021, 7:41 PM
rodwynnejones
rodwynnejones - avatar
0
I want to get a string of numbers from the user and store it in the array and then implement the QuickSort function on it
2nd Jan 2021, 7:45 PM
R SF
R SF - avatar
0
I want to run the QuickSort but my problem is that the user wants to give me a string of numbers as input
2nd Jan 2021, 7:48 PM
R SF
R SF - avatar
0
Still need more info: "12345" => {1, 2, 3, 4, 5} or "12 23 34" => {12, 23, 34} or "12 23 34" => {1 ,2, 2, 3, 3, 4}
2nd Jan 2021, 8:12 PM
rodwynnejones
rodwynnejones - avatar
0
The second one
2nd Jan 2021, 8:14 PM
R SF
R SF - avatar
0
See "12 1 45 6 -7" to {-7,1,6,12,45}
2nd Jan 2021, 8:15 PM
R SF
R SF - avatar
0
I just need a function to convert that
2nd Jan 2021, 8:16 PM
R SF
R SF - avatar
0
use strtok() to split the string and pushback into an int vector and using stoi to convert to int as you pushback. or if you need an regular array, you would need to know how may numbers that are in the) string, you could count the number of spaces + 1 (assuming single space between the ints so you can declare an array of the correct size.
2nd Jan 2021, 8:30 PM
rodwynnejones
rodwynnejones - avatar
0
Ok thanks
2nd Jan 2021, 8:31 PM
R SF
R SF - avatar