Previous Page

String converted to a character array - C++ console

Copy and Paste the following code into your favorite C++ Integrated Development Environment (IDE) - compile and run.

#include <iostream>
#include <string>

using namespace std;

int main()
    string sentence = "Hello there world";// string variable

    int size = sentence.size();// size of the string

    char charArray[size];// new character array with a size equal to the string size

     for (int i = 0; i < size; i++)
         charArray[i] = sentence[i];// The string "sentence" stored in the new
                                    // character array

      cout << sentence[16]; // output 16th index of the array

      cout << sentence[9]; // output 9th index of the array

return 0;