- The reasons for using a string class object are:
- The string class does an automatic bounds check on every index used to access string elements. This is not true for C-string, and using an invalid C-string index can result in a system crash.
- The string class automatically expands and contracts storage as needed. C-string are fixed in length and are subject to overrunning the allocated storage space.
- The string class provides a rich set of methods for operating on a string. C-string almost always require a subsidiary of functions.
- When necessary, it is easy to convert to a C-string using the string class c_str() method. Conversely, a C-string can easily be converted to a string class object by assigning it to a string object.
The reasons for using a C-string are
- The programmer has ultimate control over how the string is stored and manipulated.
- A large number of extremely useful functions exist to input, examine, and process C-strings.
- C-strings are an excellent way to explore advanced programming techniques using pointers.
- You will encounter them throughout your programming career, as they are embedded in almost all exisiting c++ code.
- They are fun to program.
