Chapter 7: Arrays and Strings
Learn how to use arrays and strings in C.
Chapter 7: Arrays and Strings
Table of Contents
- Table of Contents
- 7.1 Arrays in C
- 7.2 Strings as Character Arrays
- 7.3 Common Array and String Operations
- 7.4 Exercises
- 7.5 Solutions
- 7.6 What’s Coming in the Next Chapter
7.1 Arrays in C
Arrays store multiple values of the same type:
1
int numbers[5] = {1, 2, 3, 4, 5};
7.2 Strings as Character Arrays
Strings in C are arrays of characters ending with \0
:
1
char name[] = "Alice";
7.3 Common Array and String Operations
- Accessing elements
- Modifying values
- Iterating with loops
7.4 Exercises
- Declare an array of 10 integers and initialize it.
- Write a function to calculate the sum of an integer array.
- How do you find the length of a string in C?
7.5 Solutions
7.5.1 Solution 1
1
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
7.5.2 Solution 2
1
2
3
4
5
int sum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; i++) total += arr[i];
return total;
}
7.5.3 Solution 3
Use strlen()
from <string.h>
.
7.6 What’s Coming in the Next Chapter
Next, you’ll learn about memory management in C!
This post is licensed under CC BY 4.0 by the author.