Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,939 questions

14,849 answers

573 users

How to convert a string to int array in C++

2 Answers

0 votes
#include <iostream>

using namespace std;

void convert_string_to_int_array(string s) { 
    int len = s.length(); 
  
    int arr[len] = { 0 }; 
  
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++) { 
        if (s[i] == ',') { 
            j++; 
        } 
        else { 
            arr[j] = arr[j] * 10 + (s[i] - 48); 
        } 
    } 
  
    for (int i = 0; i <= j; i++) { 
        cout << arr[i] << " "; 
    } 
} 
  
int main() 
{ 
    string s = "1,5,9,13"; 
  
    convert_string_to_int_array(s); 
  
    return 0; 
} 



/*
run:

1 5 9 13 

*/

 




answered Aug 9 by avibootz
0 votes
#include <iostream>

using namespace std;

void convert_string_to_int_array(string s) { 
    int len = s.length(); 
  
    int arr[len] = { 0 }; 
  
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++) { 
        if (s[i] == ',' && s[i + 1] == ' ') { 
            j++;
            i++;
        } 
        else { 
            arr[j] = arr[j] * 10 + (s[i] - 48); 
        } 
    } 
  
    for (int i = 0; i <= j; i++) { 
        cout << arr[i] << " "; 
    } 
} 
  
int main() 
{ 
    string s = "1, 5, 9, 13"; 
  
    convert_string_to_int_array(s); 
  
    return 0; 
} 



/*
run:

1 5 9 13 

*/

 




answered Aug 9 by avibootz
...