Storing values into a dynamic array

This is a discussion on Storing values into a dynamic array within the C++ Programming forums, part of the General Programming Boards category; I would like to know how to store a modified string into an array of type char. Below is my ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Storing values into a dynamic array

    I would like to know how to store a modified string into an array of type char. Below is my code:

    Code:
    typedef char* ArrayPtr;
      ArrayPtr array;
      array = new char[1000];
      istringstream break_apart(input);   
      while (break_apart >> word)
      {
        front = &word.at(0); 
        rear = &word.at(word.size() - 1); 
        while (front <= rear) 
        {
              swap (*front, *rear);
              front++;
              rear--;
        }
        cout << word << " ";
      }
      
      for (int i = 0; i < input.length(); i++)
      {
          //WHAT DO I PUT HERE?!?
      }
    In the main part of the code there, I switched the positions of characters within the word to ultimately swap each word of 'input'. However, I need to store this modified string (originally 'input') into an array of type char. I know I have to use a for loop to copy it over, but what do I need to put inside the loop. I'm having trouble figuring this out... Thank you very much for all who help. I appreciate it!!!!

    FYI - this post is simply a more specific version of my previous post. I solved the palindrome instance, so I'm simplifying so you all do not have to go through already solving info to receive the needed help.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Since you are using C Style strings, you should look into strcpy/strncpy.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    i could, but I cannot cpy between type char and type string. in order for strcpy/strncpy to work i am almost positive that they have to be the same type.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    strcpy(char_array, std_string.c_str());

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    thank you very much, I solved my problem. Thanks to all who contributed.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,669
    Please don't start a new thread on a topic already being discussed.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  3. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-20-2005, 11:39 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21