Character
A character is a data type that can hold a single character.File: char1.cpp
#include <iostream> using namespace std ; int main() { char ch1 = 'a' ; char ch2 = 97 ; cout << ch1 << " " << ch2 << endl ; cout << (int)ch1 << " " << (int)ch2 << endl ; return 0 ; } $ g++ char1.cpp ; ./a.exe a a 97 97A character's underlying representation is a number. How does C++ convert that character to a number ? It uses a mapping that is an ASCII table .
https://www.ascii-code.com/
Remeber that the computer can only store 0 or 1. So a character needs to be mapped to a number and that's what the ASCII does for the English language characters. Now ASCII table does not take care of all the languages as many language need more than 256 characters however that is beyond the scope of this discussion. We can assign a number to the character variable "ch2" and it prints out the same character as "ch1" . We can also add numbers to the variables and that leads to the variables pointing to the next character according to the ASCII chart.
File: char2.cpp
#include <iostream> using namespace std ; int main() { char ch1 = 'a' ; char ch2 = 97 ; ch1 = ch1 + 1 ; ch2 = ch2 + 1 ; cout << ch1 << " " << ch2 << endl ; cout << (int)ch1 << " " << (int)ch2 << endl ; return 0 ; } $ g++ char2.cpp ; ./a.exe b b 98 98The character type is used both in "C" and "C++". We have used "cout" to print the character but we can also use the "C" style "printf" function. The below program shows how we can print the character using the "C printf" function.
File: char3.cpp
#include <iostream> using namespace std ; int main() { char ch1 = 'a' ; char ch2 = 97 ; cout << ch1 << " " << ch2 << endl ; printf( "%c %c\n" , ch1 , ch2 ) ; return 0 ; } $ ./a.exe a a a a
C Style Strings
The language "C++" includes everything in "C" include the strings used in "C". A string in "C" is an array consisting of characters with the end character being the '\0' ( null ) character which has an ASCII value of 0. This is how the system knows when the string ends. We need to be careful that we have enough space in the array if the size is allocated manually and not by the system.The below program illustrates the usage.
File: str1.cpp
<pre class="code_syntax" style="background: rgb(255, 255, 255);"><font color="#004a43">#include <iostream> #include <cstring> using namespace std ; int main() { //The below syntax automatically assigns the null character //at the end. char name1[] = "Jerry Quarry" ; //With the below style we need to manuall assign the //null character at the end char name2[] = { 'C' , 'a' , 'r' , 'l' , '\0' } ; //Safe since we have enough space in the array name3 char name3[9] = "Thompson" ; printf( "%s\n" , name1 ) ; printf( "%s\n" , name2 ) ; printf( "%s\n" , name3 ) ; cout << name1 << endl ; cout << name2 << endl ; cout << name3 << endl ; char name4[80]; strcpy (name4,"Ike "); strcat (name4,"Ibeaubuchi"); cout << name3 << " Length of name4 is: " << strlen(name4) << endl ; char name5[] = "Archie Moore continued boxing in his 40s."; char delimiters[] = " "; // Space is the delimiter char *token; // Get the first token token = strtok(name5, delimiters); // Loop through all tokens while (token != NULL) { printf("Token: %s\n", token); token = strtok(NULL, delimiters); // Use NULL for subsequent calls } return 0 ; }</font></pre> $ ./a.exe Jerry Quarry Carl Thompson Jerry Quarry 12 length of array:13 Carl 4 Thompson 8 Thompson Length of name4 is: 14 Token: Archie Token: Moore Token: continued Token: boxing Token: in Token: his Token: 40s. The line: char name1[] = "Jerry Quarry" ; Assigns the string literal "Jerry Quarry" to name1. It will allocate enough memory for "name1" including the space for the null character at the end. The line below shows a different way of constructing a string. char name2[] = { 'C' , 'a' , 'r' , 'l' , '\0' } ; In this case the system allocates memory for "name2" but we need to place the null character at the end ourselves. We can also choose to specify the size of the array ourselves. char name3[9] = "Thompson" ; We need to make sure that the size is at least as large as the literal string plus 1 . The "strcpy" copies a string from the destination to the source. strcpy (name4,"Ike "); Again we need to make sure that the variable "name4" has enough space. Similarly the "strcat" function appends the second argument to the first. Also notice that we can use both "printf" and "cout" to print the "C style string" .
C++ Style Strings
The "C++" implementation relies on a class and we do not have to worry about memory allocation and the size being large enough and so on.File: str2.cpp
#include <iostream> #include <cstring> #include <sstream> #include <string> using namespace std ; int main() { //The below syntax automatically assigns the null character //at the end. string name1( "Jerry Quarry" ) ; //With the below style we need to manuall assign the //null character at the end char name2Array[] = { 'C' , 'a' , 'r' , 'l' , '\0' } ; string name2( name2Array ) ; //Safe since we have enough space in the array name3 string name3( "Thompson" ) ; printf( "%s\n" , name1.c_str() ) ; printf( "%s\n" , name2.c_str() ) ; printf( "%s\n" , name3.c_str() ) ; cout << name1 << " " << name1.length() << endl ; cout << name2 << " " << name2.length() << endl ; cout << name3 << " " << name3.length() << endl ; string name4; name4 = "Ike " ; name4.append("Ibeaubuchi"); cout << name4 << " Length of name4 is: " << name4.length() << endl ; string name5 = "Archie Moore continued boxing in his 40s."; char delimiter = ' '; // Space is the delimiter stringstream ss( name5 ); string token; while ( getline(ss, token, delimiter)) { cout << "token:" << token << endl ; } return 0 ; } $ g++ str2.cpp ; ./a.exe Jerry Quarry Carl Thompson Jerry Quarry 12 Carl 4 Thompson 8 Ike Ibeaubuchi Length of name4 is: 14 token:Archie token:Moore token:continued token:boxing token:in token:his token:40s. The statement: string name1( "Jerry Quarry" ) ; declares an object "name1" of type string. We then initialize it with a string literal. Notice we did not have to specify or allocate the memory for the string. Also the string class has methods to obtain the length, append another string and so on. It's a huge advantage over the "C" style strings. The program shows other ways of initializing strings and a way to get the tokens out using a delimiter similar to what was done in "str1.cpp"