Convert a String to Integer Array in C/C++
Given a string str containing numbers separated with “, “. The task is to convert it into an integer array and find the sum of that array.
Examples:
Input : str = "2, 6, 3, 14"
Output : arr[] = {2, 6, 3, 14}
Sum of the array is = 2 + 6 + 3 + 14 = 25
Input : str = "125, 4, 24, 5543, 111"
Output : arr[] = {125, 4, 24, 5543, 111}
Approach:
- Create an empty array with size as string length and initialize all of the elements of array to zero.
- Start traversing the string.
- Check if the character at the current index in the string is a comma(, ). If yes then, increment the index of the array to point to the next element of array.
- Else, keep traversing the string until a ‘, ‘ operator is found and keep converting the characters to number and store at the current array element.
To convert characters to number:arr[j] = arr[j] * 10 + (Str[i] – 48)
Below is the implementation of the above idea:
// C++ program to convert a string to // integer array #include <bits/stdc++.h> using namespace std; // Fucntion to convert a string to // integer array void convertStrtoArr(string str) { // get length of string str int str_length = str.length(); // create an array with size as string // length and initialize with 0 int arr[str_length] = { 0 }; int j = 0, i, sum = 0; // Traverse the string for (i = 0; str[i] != '\0'; i++) { // if str[i] is ', ' then split if (str[i] == ', ') { // Increment j to point to next // array location j++; } else { // subtract str[i] by 48 to convert it to int // Generate number by multiplying 10 and adding // (int)(str[i]) arr[j] = arr[j] * 10 + (str[i] - 48); } } cout << "arr[] = "; for (i = 0; i <= j; i++) { cout << arr[i] << " "; sum += arr[i]; // sum of array } // print sum of array cout << "\nSum of array is = " << sum << endl; } // Driver code int main() { string str = "2, 6, 3, 14"; convertStrtoArr(str); return 0; } |
chevron_right
filter_none
Output:
arr[] = 2 6 3 14 Sum of array is = 25
Time Complexity: O(N), where N is the length of the string.
Recommended Posts:
- Convert a String to an Integer using Recursion
- Convert to Strictly increasing integer array with minimum changes
- Convert string to char array in C++
- Convert character array to string in C++
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum number of given operations required to convert a string to another string
- Convert the string into palindrome string by changing only one character.
- Convert Hexadecimal value String to ASCII value String
- Program to check if input is an integer or a string
- Rearrange a string in sorted order followed by the integer sum
- Rearrange a string in the form of integer sum followed by the minimized character
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Convert String into Binary Sequence
- Check if it is possible to convert one string into another with given constraints
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



