Hi, im pretty new to c++, and im stuck on this problem, i need to assign a letter from and array of stings to an array of chars and i just cannot figure it out.
any help is appreciated.
I tried both and both give me errors:
strcpy (newChars[j],alphabet[i]);
newChars[j]=alphabet[i];

#include <iostream>
#include <string.h>

using namespace std;

int main()
{

    char* newChars = new char[100];

    string alphabet[52] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    int i=0;
    for (int j=0;j<52;j++){
        cout << j << "======" <<  alphabet[j]<<"\n"<< endl;
        //std::strcpy (newChars[j],alphabet[i]);
        newChars[j]=alphabet[i];
    }
}

Edited 4 Years Ago by Unidennn

You could use the c_str() member function to get the character array represented by the string. Remember to delete the newChars array before exiting the program, and also to use the C++ style header <string> , not the C-style version <string.h> .

    newChars[j]= *alphabet[i].c_str();

strcpy() is wrong because it takes two character arrays as arguments, not two single characters

Your second example on line 17 is correct way to do it. But you didn't really say what the expected output should be so I can't say whether its right or wrong.

Yeah I guess using c_str() is overkill. Just posted what I thought of at first.
This should be simpler.

        newChars[j]=alphabet[j][0];

why the op is using an array of std::strings is beyond me, why not just a single string and index it as normal

std::string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

newChars[j]=alphabet[j];
This article has been dead for over six months. Start a new discussion instead.