Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,973 answers

573 users

How to convert char array to string in C++

5 Answers

0 votes
#include <iostream>

std::string convert_char_array_to_string(char *p, int len) { 
    std::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); 
  
    std::string s = convert_char_array_to_string(arr, len); 
  
    std::cout << s;
}
  
     
     
     
/*
run:
     
abcd
     
*/

 



answered Aug 10, 2019 by avibootz
edited May 10, 2024 by avibootz
0 votes
#include <iostream>

std::string convert_char_array_to_string(char *p, int len) { 
    std::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); 
  
    std::string s = convert_char_array_to_string(arr, len - 1); 
  
    std::cout << s;
}

     
     
     
/*
run:
     
c c++
     
*/

 



answered Aug 10, 2019 by avibootz
edited May 10, 2024 by avibootz
0 votes
#include <iostream>
     
int main() 
{ 
    char arr[] = "c c++";
  
    std::string s(arr);
 
    std::cout << s;
}
  
     
     
     
/*
run:
     
c c++
     
*/

 

 



answered Aug 10, 2019 by avibootz
edited May 10, 2024 by avibootz
0 votes
#include <iostream>
     
int main() 
{ 
    char arr[] = { 'a', 'b', 'c', 'd', 'e' }; 
  
    std::string s = arr; 
 
    std::cout << s;

}
  
     
     
     
/*
run:
     
abcde
     
*/

 



answered Aug 10, 2019 by avibootz
edited May 10, 2024 by avibootz
0 votes
#include <iostream>
 
int main()
{
    char arr[] = {'c', '+', '+'}; 
     
    std::string s = std::string(arr);
     
    std::cout << s;
}
 
 
 
   
/*
run:
   
c++
  
*/

 



answered May 10, 2024 by avibootz

Related questions

1 answer 246 views
5 answers 507 views
507 views asked May 10, 2021 by avibootz
1 answer 180 views
1 answer 78 views
1 answer 148 views
1 answer 150 views
150 views asked Jun 20, 2021 by avibootz
3 answers 287 views
287 views asked May 16, 2021 by avibootz
...