Thread: Storing values into a dynamic array

  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
    404
    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,268
    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,708
    Please don't start a new thread on a topic already being discussed.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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-21-2005, 12:39 AM
  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, 10:02 PM