Can a string be converted to an array of characters?

This is a discussion on Can a string be converted to an array of characters? within the C++ Programming forums, part of the General Programming Boards category; Can you break and store a string up into an array of characters?...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Can a string be converted to an array of characters?

    Can you break and store a string up into an array of characters?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    it's not that easy

    What if the string is dynamic?

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    char *buf;
    string mystring = "Hello World, I have no clue how long this is!";
    
    buf = new char[mystring.length() + 1];
    strcpy(buf, mystring.c_str());
    Like that maybe?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Program to read in input strings

    I've tried to do this but I don't know where I am going wrong. Basically I am trying to read two lines of characters from an input file. The input file is called in like this: a.out < inputfile
    For those of you who are not familar with this, this just mean you are feeding it through a file rather than through the keyboard. So
    Code:
    int some_value;
    cin << some_value
    will just make the program read the first integer and so forth.... My problem is this: I cannot read in the two character arrays successfully. The contents of the sample input file is as follows:
    Code:
    ABSDEF
    SDEFEA
    I need to read the first line into the first character array and the second line into the second character array. Please please help.

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    Code:
    cin.getline(buffer,'\n');
    //or you can also put a limit on the size
    cin.getline(buffer,sizeof(buffer)-1,'\n');
    will read one line at a time and store it in buffer.
    PHP and XML
    Let's talk about SAX

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Great! That worked. Now I just need to get the length of one of the arrays. It could be either one since both have the same length.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Thanks to everyone on this post I have this much to read in from the input file to the corresponding arrays. I do have one problem though and that is how to get the length of each of the arrays. The way that I have tried below is to use Waldo2K2's code(thank you). I made two arrays and now want to travel through them until I reach a delimiter, in this case, '\n'. Seems logical enough but its just not working.
    Code:
    int main(){
      char preorder[26];
      char inorder[26];
    
      // store values from input file into arrays
      cin.getline(preorder, '\n');
      cin.getline(inorder, '\n');
    
    
      // WHY DOESN'T THIS BLOCK OF CODE WORK TO TRAVERSE AND
      // FIND THE LENGTH OF the array(s)??
      int i = 0;
      int length = 0;
      while (preorder[i] != '\n'){
        length++;
        i++;
      }
    
      cout << "length: " << length << "\n";
    
      cout << "preorder = ";
      for (int i = 0; i < 6; i++){
        cout << preorder[i];
      }
    
      cout << "\n";
    
      cout << "inorder = ";
      for (int i = 0; i < 6; i++){
        cout << inorder[i];
      }
      cout << "\n";
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    When you read in a line
    Code:
     cin.getline(preorder, '\n');
    you read in until but not including the newline char. What you're looking for is the terminating null '\0'
    Code:
    while (preorder[i] != '\0'){
        length++;
        i++;
      }
    This can also be simplified to:
    Code:
    while (!preorder[i])++i;
    Of course you could always use strlen()

    -Futura

  9. #9
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    elaborating even further...

    the '\n' not only is seperate from the string, it still resides in the input buffer. Take the following situation.

    Code:
    cin.getline(buffer,'\n');
    [input]
    user types "hello"
    [/input]
    cout<<buffer;
    [output]
    prints "hello"
    [/output]
    cin.getline(buffer2,'\n');
    [error]
    the getline function (or any other cin) looks at the input buffer, and immediatly sees the '\n' and stops reading input, therefore the user has no time to enter a string.
    [/error]
    cout<<buffer2;
    [output]
    prints nothing
    [/output]
    
    so, after each cin call, use
    Code:
    cin.flush();
    it will wipe out anything leftover in the console input buffer.
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. string array v.s. int array
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2007, 11:07 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. 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

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