Jump to content
  • Advertisement
Sign in to follow this  
Lordimm

Character from String

This topic is 3450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to get an array of characters from a single string? What I need is the user to input a string of just one word for a game, so the user doesn't have to enter in each character. But what I need is to do is take this string and split it into an array of characters. An example would be: code, which would then be split up into (my array is called check) check[0] = c, check[1] = o, check[2] = d, and check[3] = e. How can I do this?

Share this post


Link to post
Share on other sites
Advertisement
It depends on which language you're using. Generally speaking though, a string is often a randomly accessible sequence of characters so it will behave like an array anyway. E.g. mystring[0] = 'a', mystring[1] = 'b'

Share this post


Link to post
Share on other sites
The language I am using is C++, but what you said really doesn't make sense to me.

*EDIT* So if we have:

string mystring = "code";

would mystring[0] = 'c'?

Share this post


Link to post
Share on other sites
How about this:

#include <string>
#include <iostream>

int main( )
{
std::string check;
std::cin >> check;

/* now you can access check[0], check[1] and so on */

return 0;
}


The string check will hold as many characters as the user types that don't contain whitespace.

Share this post


Link to post
Share on other sites
You can get a std::string from std::cin by doing this:

std::string userInput;
std::cin >> userInput;

Note that this will store the first word (which doesn't include white space, so if you enter "red car", userInput will be "red")

If you need to be able to get words with spaces, you can use std::getline

std::string userInput;
std::getline(std::cin, userInput);

If you want to get a C-string from std::string (that is, an array of characters that are null terminated), you can use the c_str() method. But there is probably an easier way, which is what dmatter tried to show. std::string overloads the [] operator, so you can access each character stored in a std::string as if it were an array. Example:

std::string myString = "Hello world!";
myString[2] = 'f';
std::cout << myString;

Output:

"Heflo world!";

Share this post


Link to post
Share on other sites
I know this, but I need a string to transfer their characters which are input from the user into an array of characters. Like: code

into:

mystring[0] = 'c';

mystring[1] = 'o';

mystring[2] = 'd';

mystring[3] = 'e';

Share this post


Link to post
Share on other sites
Quote:
Original post by Lordimm
Ok, thanks, that was what I wondering =).
Just make sure you make the string's size large enough so that you don't read/write past the end of the string, causing undefined behavior.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!