Strings
Objectives
We would like to :
- Investigate both C and C++ strings.
Notes
- A string is a collection of characters.
- In c
char word[] = "the string";
char * word = strdup("the string");
- Careful here, we need to delete this memory after we are done with it.
In general a c style string is an array of characters.
- But the last character is a null (0)
Manipulation of strings is most commonly done with a library function.
#include <string.h>
or #include <cstring>
reference.
- strdup makes a copy of a string
- strcat concatenates two strings
- strlen returns the length of the string.
- etc.
In modern usage c style strings are discouraged.
- They are a common source of buffer overflow errors
- Hence a source of security problems.
But at times you will need these
- If you are working in c
- If you are interfacing with c style libraries
- Or when you write assembly code!
C++ style strings
- reference.
- This is an example of a class.
- Actually a hierarchy of classes.
- A class is a collection of data and the operations on that data.
- The operations are called methods
- The class handles the memory management.
- Direct assignment and copy
- Easy string concatenation.
- There are many methods.
- Before you write a function you should check the methods
- Unless you are told not to in an assignment statement.
-
size_t
- reference
- This is an integer type
- It is unsigned as it is designed for use with arrays
- It is an abstraction, most likely a
unsigned int or unsigned long
- It will cause some compiler errors.
- What are the rules for array indexing?
- string::npos
- reference.
- This is a constant from the string library
- It is a size_t
- It is a non-existent position in a string, probably string max size + 1.
- run c+string
- Do you really want to know either of these?
- Use the symbolic names
- Why do we declare things like this?
- You should know
- size,length - return the number of characters in the string.
- [n] returns the character at a given position
- find(substr, startpos) - find the substring starting at position starpos
- Returns the starting position of the substring (>= startpos if given)
- Or string::npos if not present.
- substr(startpos, length) - extract a substring
- Starts at startpos
- If length is specified, stop there
- If length is not specified, go to the end of the string.
- sto* (stoi, stol, stoll, stof, stod, ...)
- to_string (int or floating point to string)
Some helpful character functions
- reference.
- From
#incldue <cctype>
,
- is*
- to*
- All take an int and return an int.
- But they are really character.
>>, and getline with strings
A word of caution
- Linux terminates lines in a file with \n
- Windows terminates lines in a file with \r\n
- c/c++ only expects \n
- dos2unix and unix2dos