CodeMIRAI
0 XP

Strings as character arrays

3 min read 6 runnable examples Quiz included

C has no string type. A string is an array of char that ends with a special character, '\0', the null character, which marks where the text stops.

char s[] = "Hi!"; · strlen(s) is 3, sizeof(s) is 4s[0]'H'72s[1]'i'105s[2]'!'33s[3]'\0'0char codes:the terminating zero marks where the string endsevery string function stops at '\0', so forgetting it reads past the end
A C string is a char array ending in a zero byte. Leave room for it: "Hi!" needs 4 chars, not 3.
C
#include <stdio.h>

int main() {
    char name[] = "Asha";
    printf("%s\n", name);
    printf("%c %c\n", name[0], name[3]);
    printf("size: %d bytes\n", (int) sizeof(name));    // 5: four letters plus '\0'
    return 0;
}
Diagram
index:    0     1     2     3     4
name:   ['A'] ['s'] ['h'] ['a'] ['\0']

"Asha" takes five bytes: four letters and the terminating '\0'. Every string function walks along the characters until it reaches that '\0'.

Walking through a string

C
#include <stdio.h>

int main() {
    char word[] = "banana";
    int count = 0;
    for (int i = 0; word[i] != '\0'; i++) {
        if (word[i] == 'a') count++;
    }
    printf("%s has %d a's\n", word, count);
    return 0;
}

The string.h functions

C
#include <stdio.h>
#include <string.h>

int main() {
    char first[20] = "Ada";
    char full[40];

    printf("%d\n", (int) strlen(first));       // 3: the '\0' isn't counted
    strcpy(full, first);                       // copy: full is now "Ada"
    strcat(full, " Lovelace");                 // append: "Ada Lovelace"
    printf("%s (%d characters)\n", full, (int) strlen(full));

    printf("%d\n", strcmp("apple", "banana") < 0);    // 1: apple sorts first
    printf("%d\n", strcmp("pear", "fig") > 0);        // 1: pear sorts after fig
    return 0;
}
FunctionDoes
strlen(s)counts the characters before '\0'
strcpy(dest, src)copies src into dest
strcat(dest, src)adds src onto the end of dest
strcmp(a, b)0 if equal, negative if a sorts first, positive if b does
strncpy(dest, src, n)copies at most n characters

Testing and changing characters: ctype.h

C
#include <stdio.h>
#include <ctype.h>

int main() {
    char s[] = "Hello World 2026";
    int letters = 0, digits = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        if (isalpha(s[i])) letters++;
        if (isdigit(s[i])) digits++;
        s[i] = toupper(s[i]);
    }
    printf("%s: %d letters, %d digits\n", s, letters, digits);
    return 0;
}

isalpha, isdigit, isspace, isupper and islower test a character; toupper and tolower convert one.

Reversing a string

C
#include <stdio.h>
#include <string.h>

void reverse(char s[]) {
    int i = 0;
    int j = strlen(s) - 1;
    while (i < j) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
        i++;
        j--;
    }
}

int main() {
    char word[] = "stressed";
    reverse(word);
    printf("%s\n", word);
    return 0;
}

An array is passed to a function as its address, so reverse changes the caller's string in place.

Reading a word with scanf

This example's input is Mei Tanaka:

C
#include <stdio.h>

int main() {
    char name[20];
    scanf("%s", name);          // no & here: an array name is already an address
    printf("Hello, %s!\n", name);
    return 0;
}

Only Mei was read, because %s stops at the first space. To read a whole line, including spaces, C programs use fgets:

C
char line[100];
fgets(line, sizeof line, stdin);    // reads up to 99 characters of the line
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.