Lesson 08: Strings
In the EE2049-Lab 16: The Complete Response of 2nd Order RLC Circuits, you learned about C/C++ data types. However, did you find that C/C++ does not have a data type of string? In this session, we will discuss the string and its operations in C/C++.
8.1 C-Strings (Character Arrays)
C/C++ uses character arrays to store strings instead of defining a new data type for the string. But, there is a subtle difference between character arrays and C-strings.
- A string is a sequence of zero or more characters, and the strings are enclosed in double quotation marks.
- The last character in a C-string is always the NULL character.
- A character array might not contain the NULL character.
- C-strings are stored in one-dimensional character arrays.
What difference between 'A' and "A"?
- 'A': is one character, 'A'.
- "A": is C-string that represents two characters: 'A' and '\0'.
- To store 'A', we need only one memory cell of type char.
- To store "A", we need two memory cells of type char — one for 'A' and one for '\0'.
Similarly, the C-string "Hello" represent six characters: 'H', 'e', 'l', 'l', 'o', and '\0'. To store the C-string "Hello" in computer memory, we need 6 memory cells of type char.
char str[] = "Hello";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a NULL character \0 at the end by default.
H | e | l | l | o | \0 |
The actual values stored in the memory are:
72 | 101 | 108 | 108 | 111 | 0 |
Declare a String
Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.
char string__name[size];
In the above syntax string__name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the NULL character \0 used to indicate the termination of string which differs strings from normal character arrays.
For example, declare a string to store 20 characters:
char str[21];
Initialize Strings
A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with "Apple".
char str[] = "Apple"; char str[6] = "Apple"; char str[] = {'A', 'p', 'p', 'l', 'e', '\0'}; char str[6] = {'A', 'p', 'p', 'l', 'e', '\0'}; char *str = "Apple";
Below is the memory representation of a string "Apple":
Index⇒ | str[0] | str[1] | str[2] | str[3] | str[4] | str[5] |
str | A | p | p | l | e | \0 |
Memory Address⇒ | $1000 | $1001 | $1002 | $1003 | $1004 | $1005 |
Actual Value in Memory ⇒ | 65 | 112 | 112 | 108 | 101 | 0 |
Assign Values to Strings
8.2 <string.h> Function
The C <string.h> header file declares a set of functions to work strings.
strlen()
The strlen() function calculates the length of a given string.
The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (the unsigned integer type).
Note that the strlen() function doesn't count the NULL character \0 while calculating the length.