Hi there,

I have an array of strings defined in the following way;

string wordbank[]={"yes", "no", "hello", "goodbye"};

My problem is I would like to create a seperate array of characters for each element of that array.

Something of the form; char *word_0[]={'y','e','s'}

The things I have tried so far seem to all lead to "can't change string variable type to char *". All help appreciated :)

Peter

Recommended Answers

All 5 Replies

Why? You can get every character from a string with the .at() method:

string wordbank[]= {"yes", "no", "hello", "goodbye"};
    cout << wordbank[0].at(0) << "-" << wordbank[0].at(1) << "-" << wordbank[0].at(2);

output: y-e-s

commented: Well done :) +3

>My problem is I would like to create a seperate array of characters for each element of that array.
To what end? There are ways to do what you want, but I'm not entirely sure what you want makes sense.

Thanks for replying.

It's for a group project to generate wordsearches. The guy taking over from my section asked that I give him an array for each word being input. Although would it make more sense for him to just call each character from the original wordbank[] using the .at() function?

>The guy taking over from my section asked that I give him an array for each word being input.
He probably just wants a C-style string then. You can use the c_str member function of the string class to get a C-style string. Though if I were you I'd ask the guy why he wants an "array". String objects are generally easier to work with, so it's possible he's just doing what he's comfortable with instead of looking for the best solution.

It was suitable for the application to use a for loop and select the individual characters in the string using the .at() function.

We now have a fully operational wordsearch generator. Well chuffed with it.. code just needs tidied and annotated ;)

Thanks again!

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.