SoFunction
Updated on 2025-04-13

C++ easily implements the mutual conversion of strings and character arrays

introduction

This article focuses on different ways to convert strings to char arrays and convert char arrays to strings in C++.

1. Convert strings to char arrays

C++ provides the following techniques for converting strings into char arrays:

  • Use the c_str() and strcpy() functions.
  • Use a for loop.

1.1. c_str() and strcpy() functions in C++

The C++ function c_str() and the C++ string function strcpy() can be used to easily convert strings into character arrays.

c_str()Method represents a sequence of characters in a string array followed by an empty character'\0'. It returns a null pointer to the string.

grammar:

string-name.c_str();
  • First use the c_str() method to get all characters of the string and terminate null characters.
  • Additionally, declare an empty array of type char to store the results, i.e. converting the string to the result of the char array.
  • Finally, usestrcpy()Method willc_str()The character sequence generated by the method is copied into an empty char array.

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	 
	string str = "";
	cout<<"Enter the string:\n";
	
	cin>>str;

	char arr[() + 1]; 

	strcpy(arr, str.c_str()); 
    cout<<"String to char array conversion:\n";
	for (int i = 0; i < (); i++) 
		cout << arr[i]; 

	return 0; 
}

1.2. Conversion of strings to character arrays in a for loop

To convert a char array to a string, you can use a C++ for loop.

  • First create an empty array of type char.
  • Then iterate over the input string.
  • Store characters into char array when iterating.

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	 
	string str = "";
	cout<<"Enter the string:\n";
	
	cin>>str;

	char arr[() + 1]; 
    cout<<"String to char array conversion:\n";
    for (int x = 0; x < sizeof(arr); x++) { 
        arr[x] = str[x]; 
        cout << arr[x]; 
    } 

	return 0; 
}

2. Convert char array to string

Techniques for converting char arrays to strings in C++:

  • The "+" operator.
  • C++ overloads the "=" operator.
  • C++ built-in constructor.

2.1. C++ operator ‘+’

C++ provides the function of connecting or adding data items to variables:'+' operator

  • Create a new empty string to store the result.
  • Next, use the for loop to iterate through the input char array.
  • During the process of traversing the array, use the ‘+’ operator to concatenate the characters to the string.

Example:

#include <bits/stdc++.h> 
using namespace std; 


int main() 
{ 
	char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; 
	
	int size_arr = sizeof(arr) / sizeof(char); 
	string str = ""; 
	for (int x = 0; x < size_arr; x++) { 
		str = str + arr[x]; 
	} 
	cout<<"Converted char array to string:\n";
	cout << str << endl; 
	return 0; 
}

Output:

Converted char array to string:
LIONLONG

2.2. C++ overload the '=' operator

C++ has the concept of overloading, which allows the operator to perform other operations than basic or default operations.

  • Create a new empty string.
  • use'=' operator overloadStore data items character by character to the newly created empty string.

Example:

#include <bits/stdc++.h> 
using namespace std; 


int main() 
{ 
	char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; 
	
	int size_arr = sizeof(arr) / sizeof(char); 
	string str = ""; 
	str = arr;
	cout<<"Converted char array to string:\n";
	cout << str << endl; 
	return 0; 
}

Output:

Converted char array to string:
LIONLONG

2.3. C++ string built-in constructor

In the context of converting a char array to a string, you can use the C++ string constructor.

grammar:

string string-name(char array-name);

This constructor takes a sequence of characters ending with null characters as input parameters.

Note: This can only be used when declaring strings throughout the program.

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	char arr[] = { 'L', 'I', 'O', 'N', 'L', 'O', 'N', 'G'}; 
	int size_arr = sizeof(arr) / sizeof(char); 
	string str(arr);
	cout<<"Converted char array to string:\n";
	cout <<str<< endl; 
	return 0; 
}

Output:

Converted char array to string:
LIONLONG

3. Summary

In this article, we learn about various techniques for converting strings to char arrays in C++ and vice versa.

This is the article about C++'s easy conversion of strings and character arrays. For more related contents of C++ strings and character arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!