Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Convert String to Char Array in C++
Next article icon

Convert character array to string in C++

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

This article shows how to convert a character array to a string in C++. 
The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string.
Examples: 
 

Input: char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's', '\0' } ;
Output: string s = "geeksforgeeks" ;

Input: char s[] = { 'c', 'o', 'd', 'i', 'n', 'g', '\0' } ;
Output: string s = "coding" ;


 

  • Method 1: 
    Approach: 
    1. Get the character array and its size.
    2. Create an empty string.
    3. Iterate through the character array.
    4. As you iterate keep on concatenating the characters we encounter in the character array to the string.
    5. Return the string.

Below is the implementation of the above approach.

C++
// Demonstrates conversion // from character array to string  #include <bits/stdc++.h> using namespace std;  // converts character array // to string and returns it string convertToString(char* a, int size) {     int i;     string s = "";     for (i = 0; i < size; i++) {         s = s + a[i];     }     return s; }  // Driver code int main() {     char a[] = { 'C', 'O', 'D', 'E' };     char b[] = "geeksforgeeks";      int a_size = sizeof(a) / sizeof(char);     int b_size = sizeof(b) / sizeof(char);      string s_a = convertToString(a, a_size);     string s_b = convertToString(b, b_size);      cout << s_a << endl;     cout << s_b << endl;      return 0; } 
  • Method 2: 
    The std::string has an inbuilt constructor which does the work for us. This constructor takes in a null-terminated character sequence as it’s input. However, we can use this method only at the time of string declaration and it cannot be used again for the same string because it uses a constructor which is only called when we declare a string.
    Approach: 
    1. Get the character array and its size.
    2. Declare a string (i.e, an object of the string class) and while doing so give the character array as its parameter for its constructor.
    3. Use the syntax: string string_name(character_array_name);
    4. Return the string.

Below is the implementation of the above approach.

C++
// Demonstrates conversion // from character array to string  #include <bits/stdc++.h> using namespace std;  // uses the constructor in string class // to convert character array to string string convertToString(char* a) {     string s(a);      // we cannot use this technique again     // to store something in s     // because we use constructors     // which are only called     // when the string is declared.      // Remove commented portion     // to see for yourself      /*     char demo[] = "gfg";     s(demo); // compilation error     */      return s; }  // Driver code int main() {     char a[] = { 'C', 'O', 'D', 'E', '\0' };     char b[] = "geeksforgeeks";      string s_a = convertToString(a);     string s_b = convertToString(b);      cout << s_a << endl;     cout << s_b << endl;      return 0; } 
  • Method 3: 
    Another way to do so would be to use an overloaded ‘=’ operator which is also available in the C++ std::string. 
    Approach: 
    1. Get the character array and its size.
    2. Declare a string.
    3. Use the overloaded ‘=’ operator to assign the characters in the character array to the string.
    4. Return the string.

Below is the implementation of the above approach.

C++
// Demonstrates conversion // from character array to string  #include <bits/stdc++.h> using namespace std;  // uses overloaded '=' operator from string class // to convert character array to string string convertToString(char* a) {     string s = a;     return s; }  // Driver code int main() {     char a[] = { 'C', 'O', 'D', 'E', '\0' };     char b[] = "geeksforgeeks";      string s_a = convertToString(a);     string s_b = convertToString(b);      cout << s_a << endl;     cout << s_b << endl;      return 0; } 


Next Article
Convert String to Char Array in C++

N

Nindo
Improve
Article Tags :
  • C++
  • C++ Programs
  • DSA
  • Strings
  • cpp-strings
Practice Tags :
  • CPP
  • cpp-strings
  • Strings

Similar Reads

  • Convert String to Char Array in C++
    In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++. Examples Input: str = "geeksforgeeks"Outp
    4 min read
  • Convert Vector of Characters to String in C++
    In this article, we will learn different methods to convert the vector of character to string in C++. The most efficient method to convert the vector of characters to string is by using string's range constructor. Let’s take a look at an example: [GFGTABS] C++ #include <bits/stdc++.h> using na
    2 min read
  • How to Convert a Single Character to String in C++?
    A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++. Example: Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string. Input: c = '#'Output: s = "#"Explanation: Character c is
    4 min read
  • Convert char* to std::string in C++
    Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
    3 min read
  • How to Convert Char Array to Int in C++
    In C++, a character array is treated as a sequence of characters also known as a string. Converting a character array into an integer is a common task that can be performed using various methods and in this article, we will learn how to convert char array to int in C++. Example Input:char arr1[] ="1
    2 min read
  • How to Convert a std::string to char* in C++?
    In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
    1 min read
  • How to Extract a Substring from a Character Array in C++?
    In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri
    2 min read
  • How to Compare Two Substrings in a Character Array in C++?
    In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++. Examples: Input: string: "Hello World" Substrin
    2 min read
  • How to Convert Hex String to Byte Array in C++?
    A Hex String is a combination of the digits 0-9 and characters A-F and a byte array is an array used to store byte data types only. The default value of each element of the byte array is 0. In this article, we will learn how to convert a hex string to a byte array. Example: Input: Hex String : "2f4a
    2 min read
  • Convert Float to String In C++
    In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences