
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Convert a String to Integer Array in C/C++
In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.
For this we will create a new array. Traverse through the given string, if the character is a comma “,”, we move on to the next character else add it to the new array.
Example
#include <bits/stdc++.h> using namespace std; //converting string to integer array void convert_array(string str){ int str_length = str.length(); int arr[str_length] = { 0 }; int j = 0, i, sum = 0; //traversing the string for (i = 0; str[i] != '\0'; i++) { if (str[i] == ', ') { j++; } else { arr[j] = arr[j] * 10 + (str[i] - 48); } } cout << "arr[] = "; for (i = 0; i <= j; i++) { cout << arr[i] << " "; sum += arr[i]; } cout << "\nSum of array is = " << sum << endl; } int main(){ string str = "2, 6, 3, 14"; convert_array(str); return 0; }
Output
arr[] = 1569522526 Sum of array is = 1569522526
- Related Articles
- C# Program to convert integer array to string array
- Convert integer array to string array in JavaScript?
- How to convert a string to a integer in C
- Convert an integer to a hex string in C++
- C# Program to Convert Integer to String
- C# program to convert binary string to Integer
- How to convert String to Integer and Integer to String in Java?
- How to convert Integer array list to integer array in Java?
- C++ Program to convert the string into an integer
- How to convert a string into integer in JavaScript?
- Convert Integer to Hex String in Java
- Convert string to integer/ float in Arduino
- Convert string to char array in C++
- How to convert an integer to string with padding zero in C#?
- Convert to Strictly increasing integer array with minimum changes in C++

Advertisements