- A string is a sequence of characters.
- Strings are typically enclosed in double quotes (unlike chars, which are individual letters of the alphabet enclosed usually in single quotes).
- Declaring strings:
- You can declare and initalize a string using the following syntax:
string varname = "your string here";
- You could also declare a string as a chracter array:
char varname[] = "string"
- You can spell out the individual characters when using the array-based declaration:
char varname[] = {'E','S','3,'0','1'}
- You could also declare a string with a pointer to a string:
char* varname = "DSA-2";
In all cases, you can print the string using
cout
as usual.Note that
char*
is a pointer to a constant string literal, and it is more appropriate to use the following: const char* varname = "DSA-2";
as opposed to what is written above. This particular type of declaration gives us immutable strings.