|
1 | 1 | ---
|
2 | 2 | Title: 'Strings'
|
3 |
| -Description: 'Strings are character sequences that are either expressed in double quotes or as arrays that end with a null character \0.' |
| 3 | +Description: 'Strings in C programming are used to store and manipulate sequences of characters.' |
4 | 4 | Subjects:
|
5 | 5 | - 'Code Foundations'
|
6 | 6 | - 'Computer Science'
|
7 | 7 | Tags:
|
8 |
| - - 'Strings' |
9 |
| - - 'Data Types' |
10 |
| - - 'Characters' |
11 | 8 | - 'Arrays'
|
| 9 | + - 'Characters' |
| 10 | + - 'Data Types' |
| 11 | + - 'Strings' |
12 | 12 | CatalogContent:
|
13 | 13 | - 'learn-c'
|
14 | 14 | - 'paths/computer-science'
|
15 | 15 | ---
|
16 | 16 |
|
17 |
| -**Strings** are character sequences that are either expressed in double quotes or as [arrays](https://www.codecademy.com/resources/docs/c/arrays) that end with a `null` character `'\0'`. |
| 17 | +**Strings** in C are one-dimensional arrays of characters terminated by a null character `'\0'`. They are used to store and manipulate sequences of characters such as words or sentences in C programming. |
18 | 18 |
|
19 | 19 | ## Syntax
|
20 | 20 |
|
| 21 | +To declare a string without initialization: |
| 22 | + |
21 | 23 | ```pseudo
|
22 |
| -char string-name[]; |
23 |
| -char string_name[] = "abcd"; |
24 |
| -char string_name[size] = {'a', 'b', 'c', 'd', '\0'}; |
| 24 | +char string_name[]; |
25 | 25 | ```
|
26 | 26 |
|
27 |
| -Strings in C are first declared with the `char` data type, followed by the `string_name`, and then immediately followed by square brackets `[]`. The snippet above showcases the two ways that string values are initialized: |
| 27 | +To declare and initialize a string using a string literal: |
| 28 | + |
| 29 | +```pseudo |
| 30 | +char string_name[] = "text"; |
| 31 | +``` |
| 32 | + |
| 33 | +To declare and initialize a string character by character (must include `\0` at the end): |
| 34 | + |
| 35 | +```pseudo |
| 36 | +char string_name[size] = {'c', 'h', 'a', 'r', 's', '\0'}; |
| 37 | +``` |
| 38 | + |
| 39 | +Strings in C are declared using the `char` data type, followed by the string name and square brackets `[]`. String values can be initialized in two ways: |
28 | 40 |
|
29 | 41 | - Zero or more characters, digits, and escape sequences surrounded in double quotes.
|
30 |
| -- An array of comma-separated characters, surrounded in curly brackets `{ }`, and ending with a `null` character `'\0'`\*. |
| 42 | +- An array of comma-separated characters, surrounded in curly brackets `{}`, and ending with a null character `'\0'`. |
31 | 43 |
|
32 |
| -\* This is required when an optional `size` is passed to square brackets to specify the number of characters in the string. |
| 44 | +> **Note:** The null character `'\0'` is important as it marks the end of the string. |
33 | 45 |
|
34 |
| -## Example |
| 46 | +## Example: String Declaration |
35 | 47 |
|
36 |
| -The following declaration and initialization create a string of `"Howdy"`: |
| 48 | +The following declaration and initialization create a string of "Howdy": |
37 | 49 |
|
38 | 50 | ```c
|
39 | 51 | char message[6] = {'H', 'o', 'w', 'd', 'y', '\0'};
|
40 | 52 | ```
|
41 | 53 |
|
42 |
| -Even though `"Howdy"` has only 5 characters, `message` has 6 characters due to the `null` character at the end of the `message` array. |
| 54 | +Even though "Howdy" has only 5 characters, `message` has 6 characters due to the null character at the end of the array. |
43 | 55 |
|
44 | 56 | The above statement can be rewritten as:
|
45 | 57 |
|
46 | 58 | ```c
|
47 | 59 | char message[] = "Howdy";
|
48 | 60 | ```
|
49 | 61 |
|
50 |
| -### Memory |
| 62 | +## Memory Representation |
51 | 63 |
|
52 |
| -Here's the memory presentation: |
| 64 | +Here's how a string is stored in memory: |
53 | 65 |
|
54 |
| -```pseudo |
55 |
| -Character | 'H' 'o' 'w' 'd' 'y' '\0' |
56 |
| -Index | 0 1 2 3 4 5 |
57 |
| -Address | 23451 23452 23453 23454 23455 23456 |
58 |
| -``` |
| 66 | +| Character | 'H' | 'o' | 'w' | 'd' | 'y' | '\0' | |
| 67 | +| --------- | ----- | ----- | ----- | ----- | ----- | ----- | |
| 68 | +| Index | 0 | 1 | 2 | 3 | 4 | 5 | |
| 69 | +| Address | 23451 | 23452 | 23453 | 23454 | 23455 | 23456 | |
59 | 70 |
|
60 | 71 | ## Displaying a String
|
61 | 72 |
|
62 |
| -To display a string in C, the [`printf()`](https://www.codecademy.com/resources/docs/c/basic-output) function from the **stdio.h** header file can be used along with the `%s` character to format strings, followed by the string name. |
63 |
| - |
64 |
| -### Display Example |
65 |
| - |
66 |
| -The following is an example of displaying a string in C: |
| 73 | +To display a string in C, the `printf()` function from the `stdio.h` header file can be used along with the `%s` format specifier: |
67 | 74 |
|
68 | 75 | ```c
|
69 | 76 | #include <stdio.h>
|
70 |
| - |
71 | 77 | int main() {
|
72 | 78 | char message[] = "Hi y'all!";
|
73 | 79 | printf("Bot: %s\n", message);
|
74 |
| - |
75 | 80 | return 0;
|
76 | 81 | }
|
77 |
| - |
78 | 82 | ```
|
79 | 83 |
|
80 |
| -The output would be: |
| 84 | +This code produces the following output: |
81 | 85 |
|
82 |
| -```shell |
| 86 | +``` |
83 | 87 | Bot: Hi y'all!
|
84 | 88 | ```
|
85 | 89 |
|
86 | 90 | ## String Functions
|
87 | 91 |
|
88 |
| -The **string.h** header defines a handful of string functions for manipulating arrays of characters. |
| 92 | +The `string.h` header defines several string functions for manipulating arrays of characters: |
| 93 | + |
| 94 | +- [`strchr()`](https://www.codecademy.com/resources/docs/c/strings/strchr) - Finds the first occurrence of a given character. |
| 95 | +- [`strcmp()`](https://www.codecademy.com/resources/docs/c/strings/strcmp) - Compares two strings and returns an integer value. |
| 96 | +- [`strcpy()`](https://www.codecademy.com/resources/docs/c/strings/strcpy) - Copies one string into another string. |
| 97 | +- [`strlen()`](https://www.codecademy.com/resources/docs/c/strings/strlen) - Returns the length of the string, excluding the terminating null character. |
| 98 | +- [`strtok()`](https://www.codecademy.com/resources/docs/c/strings/strtok) - Breaks a string into a series of tokens using a list of delimiters. |
| 99 | + |
| 100 | +## Frequently Asked Questions |
| 101 | + |
| 102 | +### 1. How many types of strings are there in C? |
| 103 | + |
| 104 | +In C, there is only one type of string: a null-terminated array of characters. However, strings can be implemented in two ways: |
| 105 | + |
| 106 | +- As character arrays (e.g., `char str[10] = "Hello";`) |
| 107 | +- As character pointers (e.g., `char *str = "Hello";`) |
| 108 | + |
| 109 | +### 2. What is the main difference between Java strings and C strings? |
| 110 | + |
| 111 | +The main differences between [Java strings](https://www.codecademy.com/resources/docs/java/strings) and C strings are: |
| 112 | + |
| 113 | +- Java strings are objects of the String class, while C strings are character arrays |
| 114 | +- Java strings are immutable, while C strings can be modified |
| 115 | +- Java strings have built-in methods like [`length()`](https://www.codecademy.com/resources/docs/java/strings/length), [`concat()`](https://www.codecademy.com/resources/docs/java/strings/concat), while C strings require external functions from libraries like `string.h` |
| 116 | +- Java handles memory management automatically, while C requires manual memory allocation and deallocation for dynamic strings |
| 117 | +- Java strings don't need a terminating character, but C strings must end with the null character `'\0'` |
| 118 | + |
| 119 | +### 3. What is a string literal in C? |
| 120 | + |
| 121 | +A string literal in C is a sequence of characters enclosed in double quotes (e.g., `"Hello"`) that represents a string constant. When a string literal appears in code, it is stored in read-only memory with an automatically appended null character `'\0'` at the end. String literals cannot be modified, and attempting to modify them results in undefined behavior. |
0 commit comments