打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在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:

句法:

  1. 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:

例:

  1. #include <bits/stdc .h>
  2. using namespace std;
  3. int main()
  4. {
  5. string str = '';
  6. cout<<'Enter the string:\n';
  7. cin>>str;
  8. char arr[str.length() 1];
  9. strcpy(arr, str.c_str());
  10. cout<<'String to char array conversion:\n';
  11. for (int i = 0; i < str.length(); i )
  12. cout << arr[i];
  13. return 0;
  14. }

Output:

输出:

  1. Enter the string:
  2. JournalDev
  3. String to char array conversion:
  4. 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:

例:

  1. #include <bits/stdc .h>
  2. using namespace std;
  3. int main()
  4. {
  5. string str = '';
  6. cout<<'Enter the string:\n';
  7. cin>>str;
  8. char arr[str.length() 1];
  9. cout<<'String to char array conversion:\n';
  10. for (int x = 0; x < sizeof(arr); x ) {
  11. arr[x] = str[x];
  12. cout << arr[x];
  13. }
  14. return 0;
  15. }

Output:

输出:

  1. Enter the string:
  2. JournalDev
  3. String to char array conversion:
  4. 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:

例:

  1. #include <bits/stdc .h>
  2. using namespace std;
  3. int main()
  4. {
  5. char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
  6. int size_arr = sizeof(arr) / sizeof(char);
  7. string str = '';
  8. for (int x = 0; x < size_arr; x ) {
  9. str = str arr[x];
  10. }
  11. cout<<'Converted char array to string:\n';
  12. cout << str << endl;
  13. return 0;
  14. }

Output:

输出:

  1. Converted char array to string:
  2. 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:

例:

  1. #include <bits/stdc .h>
  2. using namespace std;
  3. int main()
  4. {
  5. char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
  6. int size_arr = sizeof(arr) / sizeof(char);
  7. string str = '';
  8. str = arr;
  9. cout<<'Converted char array to string:\n';
  10. cout << str << endl;
  11. return 0;
  12. }

Output:

输出:

  1. Converted char array to string:
  2. 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:

句法:

  1. 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:

例:

  1. #include <bits/stdc .h>
  2. using namespace std;
  3. int main()
  4. {
  5. char arr[] = { 'J', 'O', 'U', 'R', 'N', 'A', 'L', 'D', 'E', 'V' };
  6. int size_arr = sizeof(arr) / sizeof(char);
  7. string str(arr);
  8. cout<<'Converted char array to string:\n';
  9. cout <<str<< endl;
  10. return 0;
  11. }

Output:

输出:

  1. Converted char array to string:
  2. 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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C语言 字符指针和字符数组的区别
给字符数组赋值的方法
C字符数组和C++字符串
C 之string学习笔记(1)
华为经典C语言面试题
c++中c_str()的用法详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服