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 provides us with the following techniques to convert a string to char array:
C 为我们提供了以下技术,可将字符串转换为char数组:
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();
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
For the conversion of char array to a string, we can use C for loops with ease.
为了将char数组转换为字符串,我们可以轻松地将C 用于循环 。
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
The mentioned techniques can be used to convert a char array to string in C :
提到的技术可用于在C 中将char数组转换为字符串:
C provides us with ' ' operator
to concatenate or add data items to a variable.
C 为我们提供了' ' 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 = '';
- 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
C has got the concept of overloading which makes an operator perform other operations apart from the basic or default operation.
C 具有重载的概念,它使操作员可以执行除基本或默认操作之外的其他操作。
'=' 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
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
In this article, we have understood various techniques to convert string to char array and vice-versa in C .
在本文中,我们了解了在C 中将字符串转换为char数组(反之亦然)的各种技术。
翻译自: https://www.journaldev.com/37220/convert-string-to-char-array-c-plus-plus
联系客服