String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; Ok, I have to try and code my own string class. I am doing a char array of course to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    String Class

    Ok, I have to try and code my own string class. I am doing a char array of course to store the characters. Is there a way to allocate memory as needed? So i do not have to know the predefined length? pretty much like a vector but dont think we can use one.

    Is there a limit to the string class and how long a string can be?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Bitphire
    Is there a limit to the string class and how long a string can be?
    Code:
    #include <iostream>
    #include <string>
    
    ...
    
    std::string str;
    std::cout << "Max size is: " << str.max_size() << std::endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    Create a new string class?? What for? Isn't it already created

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Teacher is trying to get us to understand OOP better. Says we are still not programming the OOP way or something of the sort.

    So got any ideas on how to do this? LOL not the whole thing just trying to beable to get a char array from user but dont know how long the input will be. I am not sure if we can use vectors or not but if we can that might be easier :P

    Edit:
    How can I find the end of a char array?
    Code:
    tried while(charray[i]), while(charray[i] != NULL), and while(charray[i] != '\0')
    
    when charray was set to char charray[3]={'Y','o','u'};
    
    and did a cout i got "You(crap)(crap)(crap)". It tried to output 6 even though it should only be 3? I dont know why. If you need more let me know
    Last edited by Bitphire; 02-24-2005 at 10:40 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I always wanted to make my own String class. Just to know how it would be implemented and to say I can do it. and for the knowledge. Im not a student just a hobbiest. I would say you allocate memory dynamically. I think if you need to allocate more memory as needed. You would have to copy the string into another dynamic char array. but I wonder if you can append new memory to the other dynamic char array. that would be cool. Well those are my thoughts on the matter. Of course im not that helpful cause of a beginner. but I like commenting
    lets see if I can make up any code.
    Code:
    int size;
    char s1[]={'h','e','l','l','o'};
    size=sizeof(s1)/sizeof(s1[0]);
    char *MyString=new char[size];
    for(int i=0;i<size;i++){
    MyString[i]=s1[i];
    }
    for(int i=0;i<size;i++){
    cout<<MyString[i];
    }
    ok that isnt a class or anything. Unsure if it will even help you but ok byebye im out

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    LOL, thanks will give it a try and see what happens

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char charray[3]={'Y','o','u'};
    This doesn't terminate your string, it just stores three characters in your array. You need room for the string terminator. The easiest way to initialize a string is this:
    Code:
    char charray[4] = "You";
    This automatically adds the string terminator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM

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