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];
    }
}

Recommended Answers

All 7 Replies

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();

Just noticed, there seems to be a small typo.
It should be
newChars[j]= *alphabet[j].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.

thank you very much, that helped alot :)

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

Too complicated, you don't need to use c_str()

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];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.