DMarlon Studio

Lesson 11 · 11 of 20

Strings as char arrays · C

Work with text in C as char arrays that end in '\0', using the string.h functions: strlen, strcpy, strcat and strcmp.

C · Strings as char arrays in DMarlon Studio: the example's code and its output in the terminal

In C, a piece of text, or string, is an array of char that ends with the special character `'\0'`. char city[20] = "Madrid" takes 20 bytes but only uses 7: the six letters and the '\0'. `strlen` counts the letters up to the '\0' (6), whereas sizeof gives the size of the array (20). As with any array, each letter is read by its index: city[0].

An array cannot be copied with =. That is what `strcpy(destination, source)` from string.h is for. The copy is independent: changing copy[0] to 'm' leaves city alone. `strcat` adds one text at the end of another. In both cases the destination must have enough room; otherwise it would write outside the array.

Strings are not compared with == either. `strcmp(a, b)` returns 0 when they are equal, a negative number when a comes first in alphabetical order and a positive one when it comes after. Since "pear" comes after "apple", the last else wins.

To walk through a string, just move forward until you find the '\0': the for at the end does exactly that and counts the letter o in greeting.

Code · main.c

#include <stdio.h>#include <string.h>int main(void) {    // A string is a char array that ends with the character '\0'.    char city[20] = "Madrid";    printf("%s has %zu letters\n", city, strlen(city));    printf("The array has room for %zu chars\n", sizeof(city));    printf("First: %c, last: %c\n", city[0], city[strlen(city) - 1]);    // = cannot copy an array: strcpy copies the text.    char copy[20];    strcpy(copy, city);    copy[0] = 'm';    printf("Original: %s, copy: %s\n", city, copy);    // strcat adds text at the end (there must be room for it).    char greeting[40] = "Hello from ";    strcat(greeting, city);    printf("%s\n", greeting);    // strcmp gives 0 if equal, <0 or >0 for alphabetical order.    char a[] = "pear", b[] = "apple";    if (strcmp(a, b) == 0) printf("Same fruit\n");    else if (strcmp(a, b) < 0) printf("%s comes before %s\n", a, b);    else printf("%s comes before %s\n", b, a);    // Walk the string until '\0', counting the letter 'o'.    int found = 0;    for (int i = 0; greeting[i] != '\0'; i++)        if (greeting[i] == 'o') found++;    printf("'%s' has the letter o %d times\n", greeting, found);    return 0;}

Output · cc main.c -o main && ./main

Madrid has 6 letters
The array has room for 20 chars
First: M, last: d
Original: Madrid, copy: madrid
Hello from Madrid
apple comes before pear
'Hello from Madrid' has the letter o 2 times

Key points

  • A string is a char array that ends in '\0'.
  • strlen counts letters; sizeof measures the array.
  • strcpy copies and strcat appends: the destination needs room.
  • strcmp gives 0 when the texts are equal.
  • Strings are not copied with = or compared with ==.

Exercise

Walk through greeting and turn every lowercase letter into uppercase by subtracting 'a' - 'A'; then print it (you should get HELLO FROM MADRID).