473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert String to char

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
5 3084
weaknessforcats
9,208 Expert Mod 8TB
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
9966
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
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
9966
16
I see.. okay I'll try to implement it onto my code. Thanks a lot
Oct 4 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.