By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Convert String to char

P: 16
Hi, currently I'm having a problem on converting a string type array into a char type. An example is as follows:

string fruits[20];
vector <char> temp;

The fruits array HAS to be a string type and temp vector HAS to be a char type. This can't be changed. So I've a problem on storing the element of fruits array into temp vector as follows:

temp.push_back(fruits[0]); <-----I know this is wrong.

So can I know how to convert string array into char type? Thank you
Oct 4 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Are you sure about this??

A fruits element might be PEACH. All you can out in the vector is one letter, liked the P or A or the H.

However, if fruits[0] is a string then fruits[0][0] is the first character in the string.
Oct 4 '07 #2

P: 16
hmm... yes actually that's what I want. Because initially I've stored all the fruit's name into a string array and it's used by other function. And somewhere down the whole code, I need to compare character by character of the fruit's name. For example:

1. P A P A Y A
2. P E A C H

I need to compare "P" with "P", since it's the same, then I've to proceed on the second alphabet which is "A" and "E" and so on...

And I don't understand why you said fruits[0][0] will be equal to "P". Could you please explain it further? Sorry I'm just a new learner..
Oct 4 '07 #3

Scieck
P: 10
hmm... yes actually that's what I want. Because initially I've stored all the fruit's name into a string array and it's used by other function. And somewhere down the whole code, I need to compare character by character of the fruit's name. For example:

1. P A P A Y A
2. P E A C H

I need to compare "P" with "P", since it's the same, then I've to proceed on the second alphabet which is "A" and "E" and so on...

And I don't understand why you said fruits[0][0] will be equal to "P". Could you please explain it further? Sorry I'm just a new learner..
You can imagine an array of string as an array of arrays, or a 2 dimensional array, if for example we have:

Expand|Select|Wrap|Line Numbers
  1. string fruits[3];
  2. fruits[0] = "peach";
  3. fruits[1] = "grape";
  4. fruits[2] = "kiwi";
  5.  
if you take fruits at position [0][0] you have char 'p', at position [0][1] you have the char 'e', at position fruits[2][2] you have char 'w'.
by looping through the fruits 2d array you can access all the chars.
this:
Expand|Select|Wrap|Line Numbers
  1. printf("%c", fruits[1][2]);
  2.  
will print the char 'a'.
Oct 4 '07 #4

P: 16
I see.. okay I'll try to implement it onto my code. Thanks a lot
Oct 4 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
1. P A P A Y A
2. P E A C H
It looks like you only need a vector<string>.

Expand|Select|Wrap|Line Numbers
  1. vector<string> fruits;
  2.  
Can you not compare your names by:
Expand|Select|Wrap|Line Numbers
  1. if (fruits[1] == fruits[2])  // or fruits[1] < fruits[2])  etc...
  2. {
  3. //etc..
  4.  
??
Oct 4 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.