在C ++中将String转换为Char数组并将Char数组转换为String

In this article, we will be focusing on the different ways to convert String to char array and char array to String in C++. While dealing with String data, we may need to convert the string data items to character array and vice-versa. This tutorial will help you solve exactly that.

在本文中,我们将重点介绍C ++中将String转换为char数组和将char数组转换为String的不同方法 在处理字符串数据时,我们可能需要将字符串数据项转换为字符数组,反之亦然。 本教程将帮助您完全解决该问题。



在C ++中将字符串转换为Char数组 (Convert String to Char Array in C++)

C++ provides us with the following techniques to convert a string to char array:

C ++为我们提供了以下技术,可将字符串转换为char数组:

  • Using c_str() and strcpy() function

    使用c_str()和strcpy()函数
  • Using a for loop

    使用for循环


1. C ++中的c_str()和strcpy()函数 (1. The c_str() and strcpy() function in C++)

C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily.

C ++ c_str()函数以及C ++字符串strcpy()函数可用于轻松地将字符串转换为char数组。

The c_str() method represents the sequence of characters in an array of string followed by a null character (‘\0’). It returns a null pointer to the string.

c_str()方法表示字符串数组中的字符序列,后跟一个空字符('\ 0') 。 它返回指向该字符串的空指针。

Syntax:

句法:


string-name.c_str();
  • At first, we use c_str() method to get all the characters of the string along with a terminating null character.

    首先,我们使用c_str()方法获取字符串的所有字符以及一个终止的空字符。
  • Further, we declare an empty array of type char to store the result i.e. result of the conversion of string to char array.

    此外,我们声明了一个char类型空数组来存储结果,即将字符串转换为char数组的结果。
  • Finally, we use strcpy() method to copy the character sequence generated by the c_str() method to the empty char array.

    最后,我们使用strcpy()方法将c_str()方法生成的字符序列复制到空char数组中。

Example:

例:


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

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

	char arr[str.length() + 1]; 

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

	return 0; 
} 

Output:

输出:


Enter the string:
JournalDev
String to char array conversion:
JournalDev


2.在C ++中使用for循环将字符串转换为Char数组 (2. String to Char Array Conversion in C++ Using for Loop in)

For the conversion of char array to a string, we can use C++ for loops with ease.

为了将char数组转换为字符串,我们可以轻松地将C ++用于循环

  • Initially, we create an empty array of type char

    最初,我们创建一个类型为char的空数组
  • After this, we iterate through the input string

    之后,我们遍历输入字符串
  • While iterating, we store the characters into the char array

    进行迭代时,我们将字符存储到char数组中

Example:

例:


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

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

	char arr[str.length() + 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; 
} 

Output:

输出:


Enter the string:
JournalDev
String to char array conversion:
JournalDev


在C ++中将Char数组转换为字符串 (Convert Char Array to String in C++)

The mentioned techniques can be used to convert a char array to string in C++:

提到的技术可用于在C ++中将char数组转换为字符串:

  • The ‘+’ operator

    “ +”运算符
  • C++ overloaded ‘=’ operator

    C ++ 重载的'='运算符
  • C++ inbuilt-constructor

    C ++ 内置构造函数


1. C ++“ +”运算符 (1. C++ ‘+’ operator )

C++ provides us with '+' operator to concatenate or add data items to a variable.

C ++为我们提供了'+' operator以将数据项连接或添加到变量。

  • We create a new empty string to store the result.

    我们创建一个新的空字符串来存储结果。
  • Taking it ahead, we use a for loop to traverse through the input char array.

    首先,我们使用for循环遍历输入char数组。
  • In the process of traversing through the array, we use ‘+’ operator to concatenate the characters to the string.

    在遍历数组的过程中,我们使用'+'运算符将字符连接到字符串。

Example:

例:


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


int main() 
{ 
	char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' }; 
	
	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:
JOURNALDEV


2. C ++重载“ =”运算符 (2. C++ overloaded ‘=’ operator)

C++ has got the concept of overloading which makes an operator perform other operations apart from the basic or default operation.

C ++具有重载的概念,它使操作员可以执行除基本或默认操作之外的其他操作。

  • Initially, we create a new empty string.

    最初,我们创建一个新的空字符串。
  • We use the overloaded '=' operator to store the data items character by character into the newly created empty string.

    我们使用重载的'=' operator将数据项逐个字符存储到新创建的空字符串中。

Example:

例:


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


int main() 
{ 
	char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' }; 
	
	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:
JOURNALDEV


3.C ++ String内置构造函数 (3.C++ String inbuilt constructor)

In the context of conversion of char array to string, we can use C++ String Constructor for the same.

在将char数组转换为字符串的上下文中,我们可以使用C ++字符串构造函数

Syntax:

句法:


string string-name(char array-name);

This constructor takes a sequence of characters terminated by a null character as an input parameter.

此构造函数将以空字符结尾的字符序列作为输入参数。

Note: This constructor string string() can be used only at the time of string declaration throughout the program.

注意 :此constructor string string()仅可在整个程序中声明字符串时使用。

Example:

例:


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

int main() 
{ 
	char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' }; 
	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:
JOURNALDEV


结论 (Conclusion)

In this article, we have understood various techniques to convert string to char array and vice-versa in C++.

在本文中,我们了解了在C ++中将字符串转换为char数组(反之亦然)的各种技术。



参考资料 (References)

翻译自: https://www.journaldev.com/37220/convert-string-to-char-array-c-plus-plus

  • 14
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值