Skip to the task
Layout
Back to classic view
READTask 1 of 20 · step 1: A string is a char arrayA string is a char array
A string is a char array in practice/06/

A string is a char array with '\0' at the end. strlen and sizeof, %s, literals and const char *, Lithuanian letters at 2 bytes each.

Details & hint

Until now, text lived only in printf's quotes. In this topic you keep text in variables, and at the end you write your own input.h.

A string is a char array with '\0'

C has no separate string type. A string is a char array whose text ends with a special character '\0' (a byte equal to 0). That is how every function knows where the text ends.

char name[8] = "Ona";
'O''n''a''\0'???? 012347 strlen(name) = 3sizeof name = 8 "ąžuolas" in UTF-8: ąžuolas'\0' 7 letters, but strlen("ąžuolas") = 9: ą and ž take 2 bytes each
  • "Ona" in quotes is a string literal: 3 letters and a '\0' at the end, 4 bytes in all.
  • The array may be bigger than the text: the remaining bytes are unused.
  • The array must be at least one byte bigger than the text: '\0' takes room too.

Length: strlen and sizeof

what it counts name
strlen(name) the bytes up to '\0' 3
sizeof name the size of the whole array 8

strlen (from <string.h>) walks the text up to '\0' every time, so it's worth keeping its result in a variable rather than calling it in a loop condition on every pass.

Lithuanian letters

CLion saves files in UTF-8: a Latin letter is 1 byte, and ą, ž, ė and the other Lithuanian ones are 2 each. A C string counts bytes, not letters, so strlen("ąžuolas") ("oak") is 9 although it has 7 letters. That matters when you count room in an array: the name "Žydrūnė" needs 10 bytes plus one more for '\0'.

Printing: %s

printf("Hello, %s!\n", name);

%s prints the bytes from the start of the array up to '\0'. If there is no '\0', printf carries on through memory printing garbage – or the program crashes.

A literal and const char *

const char *city = "Kaunas";

The literal "Kaunas" sits in memory that can't be changed. A pointer to it is written with const: then the compiler won't let you write through it. If the text needs changing, copy it into an array of your own – char city[20].

practice 06 hall.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#define ROWS 4
#define SEATS 6
/* How many free seats (0) there are in a row.
Before: 0 <= row < ROWS.
After: a number from 0 to SEATS. */
int free_seats(int hall[][SEATS], int row) {
assert(row >= 0 && row < ROWS);
int count = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[row][s] == 0) {
count++;
}
}
return count;
}
/* The first row that has k free seats side by side.
Before: 1 <= k <= SEATS.
After: the row's index, or -1 if there is none. */
int find_row(int hall[][SEATS], int k) {
assert(k >= 1 && k <= SEATS);
for (int r = 0; r < ROWS; r++) {
int in_a_row = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 0) {
in_a_row++;
if (in_a_row == k) {
return r;
}
} else {
in_a_row = 0;
}
}
}
return -1;
}
int main(void) {
int hall[ROWS][SEATS] = {
{1, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0},
};
for (int r = 0; r < ROWS; r++) {
printf("Row %d: ", r + 1);
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 1) {
printf("#");
} else {
printf(".");
}
}
printf(" free: %d\n", free_seats(hall, r));
}
for (int k = 2; k <= 5; k++) {
int r = find_row(hall, k);
if (r == -1) {
printf("No %d seats side by side.\n", k);
} else {
printf("%d seats side by side: row %d.\n", k, r + 1);
}
}
return 0;
}