Some help with strings/char arrays

This is a discussion on Some help with strings/char arrays within the C++ Programming forums, part of the General Programming Boards category; I was wondering how i could make an array that would adapt itself for any amount of characters. I hate ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Some help with strings/char arrays

    I was wondering how i could make an array that would adapt itself for any amount of characters. I hate making a char array only so long. I have heard that strings help with this. How do i implement strings AND manipulate them. I cant do some of the things i need to do with strings that i can with char arrays (Like Encryption for instance) Any help would be greatly appreciated. Thanks!

  2. #2
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    A char array is a static buffer that allocates x number of bytes on the programs stack (x being the number in char asdf[x]; when declaring a char array), the std::string class differs from char arrays in the sense that it dynamically allocates memory for you on initialization, this way people cannot execute standard stack based buffer overflows into your code.

    In C if you do this:
    Code:
    char static_important_data[] = "aaaaaaaaaa";
    char user_defined_data[50];
    ...
    scanf("%s\n",user_defined_data);
    printf("%s",static_important_data);
    printf("%s",user_defined_data);
    This program could go as expected as planned or it could go horribly wrong. If someone enters 55 characters (let's say all B's) on that scanf call, what will that "static_important_data" var be? It'll be aaaaabbbbb (or something not far removed.)

    Contra C++ and std::string where I can do this:
    Code:
    std::string static_important_data = "aaaaaaaaaa";
    std::string user_defined_data;
    
    ...
    
    std::cin>>user_defined_data;
    std::cout<<static_important_data;
    std::cout<<user_defined_data;
    And I give that cin call
    asdkifjaowiejflewkdfjsldkfjasldkjflaskdjflaskdjfal sdkfmasldkfjasdo98fausdflijasdlfkjasdlfusaodfijasd lfkjasdlfkjasodlkfjasldfkajsdlfkajsd
    It'll still print out those ten "a"s when you call that cout because std::string automatically adjusts itself to when it's assigned. Another slight advantage is that with std::string you can do integer like comparison because it overloads the == operator.

    For more information see here.
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ill try this out. But if i were encrypting it like this:
    Code:
    string text;
    string key;
    string enc_text;
    ...
    
    for(int i=0; i<(strlen(text)); i++)
    {
         enc_text[i]=text[i]^key[i];
    }
    
    ...
    Would that work?

  4. #4
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    Refer to the link I gave you at the bottom of my post, you should be able to figure it out from there. A trick you might want to use is to simple 'disassemble' your string into a vector, edit it, then reassemble it back into another string which is a fairly simple process.
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    All righty then, ill give it a good read. And cross my fingers. Ive never worked with vectors before... Thanks!

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Why wont this work? Do strings not accept spaces? It works if everything doesnt have a space in it, but when i put a space in the program just exits right after i hit enter.

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        string text;
        cin>>text;
        cin.ignore();
        cout<<"\n\n"<<text;
        cin.get();
        return 0;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Junior89
    Why wont this work? Do strings not accept spaces?
    Sure. But input via cin is whitespace delimited. If you want to read a line of text, use getline.

    [edit]Also, std::string is in <string>, not <string.h>.
    Last edited by Dave_Sinkula; 10-01-2005 at 09:10 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I was thinking of using cin.getline() but one dilemma. When you use it it is supposed to look like this:
    Code:
    string text;
    ...
    cin.getline(text, # HERE);
    ...
    Because a string doesnt have a set # of characters, what do i put there? Do i negate it entirely? Thanks!

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    getline is overloaded for strings:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string text;
       if ( getline(cin, text) )
       {
          cout << text << '\n';
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Does that read from or write to text? Ill try it to see.

    EDIT: SO it does both? Ok so we are using the getline to get the line typed in above it right?
    Last edited by Junior89; 10-01-2005 at 09:43 PM.

  11. #11
    Registered User byte's Avatar
    Join Date
    Aug 2005
    Posts
    8
    Use pointer.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    getline reads a line of text into a string. There are two versions: cin.getline(...) and getline(cin, ...). Since you want to be using the C++ string class, you use the second version as has been shown. It does the same thing as cin>>, except it doesn't stop at the first space, it stops at the first newline.

    >> Use pointer
    Don't use a pointer.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Quote Originally Posted by Junior89
    Does that read from or write to text? Ill try it to see.

    EDIT: SO it does both? Ok so we are using the getline to get the line typed in above it right?
    Yup ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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