Convert String to char

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 9966
    New Member
    • Sep 2007
    • 16

    Convert String to char

    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • 9966
      New Member
      • Sep 2007
      • 16

      #3
      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..

      Comment

      • Scieck
        New Member
        • Oct 2007
        • 10

        #4
        Originally posted by 9966
        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:

        Code:
        string fruits[3];
        fruits[0] = "peach";
        fruits[1] = "grape";
        fruits[2] = "kiwi";
        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:
        Code:
        printf("%c", fruits[1][2]);
        will print the char 'a'.

        Comment

        • 9966
          New Member
          • Sep 2007
          • 16

          #5
          I see.. okay I'll try to implement it onto my code. Thanks a lot

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by 9966
            1. P A P A Y A
            2. P E A C H
            It looks like you only need a vector<string>.

            [code=c]
            vector<string> fruits;
            [/code]

            Can you not compare your names by:
            [code=cpp]
            if (fruits[1] == fruits[2]) // or fruits[1] < fruits[2]) etc...
            {
            //etc..
            [/code]

            ??

            Comment

            Working...