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 char array to string in C++

4 Answers

0 votes
#include <iostream>
    
using namespace std;
    
string convert_char_array_to_string(char *p, int len) { 
    string s = ""; 
     
    for (int i = 0; i < len; i++) { 
        s = s + p[i]; 
    } 
    return s; 
} 
   
int main() 
{ 
    char arr[] = { 'a', 'b', 'c', 'd' }; 
 
   
    int len = sizeof(arr) / sizeof(char); 
 
    string s = convert_char_array_to_string(arr, len); 
 
    cout << s;
 
    return 0; 
}
 
    
    
    
/*
run:
    
abcd
    
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
    
using namespace std;
    
string convert_char_array_to_string(char *p, int len) { 
    string s = ""; 
     
    for (int i = 0; i < len; i++) { 
        s = s + p[i]; 
    } 
    return s; 
} 
   
int main() 
{ 
    char arr[] = "c c++";
 
    int len = sizeof(arr) / sizeof(char); 
 
    string s = convert_char_array_to_string(arr, len - 1); 
 
    cout << s;
 
    return 0; 
}
 
    
    
    
/*
run:
    
c c++
    
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
    
using namespace std;


int main() 
{ 
    char arr[] = "c c++";
 
    string s(arr);

    cout << s;
 
    return 0; 
}
 
    
    
    
/*
run:
    
c c++
    
*/

 




answered Aug 10 by avibootz
0 votes
#include <iostream>
    
using namespace std;


int main() 
{ 
    char arr[] = { 'a', 'b', 'c', 'd', 'e' }; 
 
    string s = arr; 

    cout << s;
 
    return 0; 
}
 
    
    
    
/*
run:
    
abcde
    
*/

 




answered Aug 10 by avibootz
...