Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

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

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

11,818 questions

16,089 answers

573 users

How to convert string to char array in C++

6 Answers

0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    string s = "c c++ java php"; 
    char arr[s.length()]; 
   
    for (int i = 0; i < sizeof(arr); i++) { 
        arr[i] = s[i]; 
    } 
     
    for (int i = 0; i < sizeof(arr); i++) { 
        cout << arr[i] << endl;
    } 
}
 
 
 
/*
run:
 
c
  
c
+
+
  
j
a
v
a
  
p
h
p
 
*/

 




answered Aug 10 by avibootz
edited Aug 10 by avibootz
0 votes
#include <iostream>

using namespace std;

int main() {
    string s = "c c++ java php"; 
    char arr[s.length()]; 
  
    strcpy(arr, s.c_str()); 
    
    for (int i = 0; i < sizeof(arr); i++) { 
        cout << arr[i] << endl;
    } 
}



/*
run:

c
 
c
+
+
 
j
a
v
a
 
p
h
p

*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    string s = "c c++ java php"; 
    char arr[s.length()]; 
   
    strncpy(arr, s.c_str(), sizeof(arr));
     
    for (int i = 0; i < sizeof(arr); i++) { 
        cout << arr[i] << endl;
    } 
}
 
 
 
/*
run:
 
c
  
c
+
+
  
j
a
v
a
  
p
h
p
 
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    string s = "c c++ java php"; 

    char *p = new char [s.length() + 1];
    strcpy (p, s.c_str());
     
    for (int i = 0; i < s.length(); i++) { 
        cout << p[i] << endl;
    } 
    
    delete p;
}
 
 
 
/*
run:
 
c
 
c
+
+
 
j
a
v
a
 
p
h
p
 
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    string s = "c c++ java php"; 

    const char *p = s.c_str();
    
    s[0] = 'A';
     
    for (int i = 0; i < s.length(); i++) { 
        cout << p[i] << endl;
    } 
}
 
 
 
/*
run:
 
A
 
c
+
+
 
j
a
v
a
 
p
h
p
 
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    string s = "c c++ java php"; 

    char *p = &s[0];
    
    s[0] = 'A';
     
    for (int i = 0; i < s.length(); i++) { 
        cout << p[i] << endl;
    } 
}
 
 
 
/*
run:
 
A
 
c
+
+
 
j
a
v
a
 
p
h
p
 
*/

 




answered Aug 10 by avibootz
...