How to convert part of a string to char array in C++

1 Answer

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

 



answered Aug 10, 2019 by avibootz

Related questions

1 answer 372 views
2 answers 382 views
1 answer 304 views
304 views asked Oct 5, 2016 by avibootz
1 answer 198 views
198 views asked Jan 13, 2017 by avibootz
1 answer 338 views
1 answer 257 views
...