Convert String to Char Array using C++ | RRTUTORS

Published December 29, 2020

If you are a programmer then you might have encountered this error ‘cannot convert std: string to char* data type’. In many programming codes, we have to convert the string into a char array or vice versa but to execute this we have to follow the long lines of codes. Is there any short way to convert string to char array using C++?  Yes, there are many ways to program this.

 

If you are a beginner programmer, you want to create a project with C++, Refer LibraryManagement Project with C++

 

Here we have mentioned different methods from which you can easily convert String to char array using C++

 

Example:

Input: string s = "rrtutors";

Output: char s[] = { 'r', 'r', 't', 'u', 't', 'o', 'r',

                     's'};

Input: string s = "program";

Output: char s[] = { 'p', 'r', 'o', 'g', 'r', 'a', 'm' } ;

 

 

3 Methods To Convert String to Char Array using C++

 

1. Using c_str() and strcpy() function in C++

To convert string into char array using C++ we can use c_str() and strcpy() function.
c_str() function can be utilized to get back a pointer into a variety which includes null characters symbolizing the initial value of the string.

Algorithm:

  • We will use c_str() method to extract all the characters of the string along with a null character that is terminating.

  • Then we will declare an empty array of char type to store the output i.e. output of the converting string to char array.

Now at last, we will use strcpy() method to copy the character sequence given by the c_str() function to an empty char array

 

Syntax

const char* c_str() const ;

 

Keep in mind that the length of the char array should not be less than the length of the input string. Here is the program for this method

 

Program:

// Program to convert a string into char array using C++

#include <bits/stdc++.h>

 

using namespace std;

 

// Main code

int main()

{

// Assign the value to string

string s = "rrtutors";

 

int n = s.length();

 

// Declare the character array

char char_array[n + 1];

 

// Now copy the characters of the string to char array

strcpy(char_array, s.c_str());

 

for (int i = 0; i < n; i++)

cout << char_array[i];

 

return 0;

}

 

Output:

rrtutors

 

2.  To Convert String to Char Array using For Loop

Algorithm:

  • First, we will create an empty array of char type.

  • Now, iterate through the input string.

  • While iterating the input string, we have to store the characters into the char array.

Program:

#include <bits/stdc++.h> 

using namespace std; 

 

int main() 

      

    string str = "";

    cout<<"Enter your string:\n";

     

    cin>>str;

 

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

    cout<<"The output of 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 your string:                                                                                                                            

RRTUTORS                                                                                                                                      

The output of string to char array conversion:                                                                                                

RRTUTORS 

 

3.  To Convert String to Char Array by assigning the address

Algorithm:

  • Take an input string and store it in the variable.

  • Assign the address of the first character of the string to a pointer of char.

  • Print the char array as an output

Program:

#include <cstring>

#include <iostream>

#include <string>

using namespace std;

 

// Driver Code

int main()

{

char* char_arr;

string str_obj("RRTUTORS");

char_arr = &str_obj[0];

cout << char_arr;

return 0;

}

 

Output:

RRTUTORS

 

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1188 Views