Aral Code

Arrays & Functions Lesson 10 of 16

C Strings — char Arrays and string.h

How C stores text in char arrays ending with '\0', and how to use strlen, strcpy, strcat, strcmp, toupper and fgets safely.

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

How strings are stored

char name[] = "Ana";
index:   0     1     2     3
value:  'A'   'n'   'a'  '\0'

"Ana" has 3 letters but needs 4 bytes, because of the '\0' at the end. Functions like printf("%s") print characters until they reach '\0'.

Declaring strings

#include <stdio.h>

int main(void) {
    char school[] = "Rizal NHS";     /* size 10, chosen automatically */
    char section[20] = "Mabini";     /* room for up to 19 characters */
    printf("%s - %s\n", school, section);
    printf("First letter: %c\n", school[0]);
    return 0;
}

Output

Rizal NHS - Mabini
First letter: R

string.h functions

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

int main(void) {
    char first[30] = "Juan";
    char last[] = "Dela Cruz";
    char full[60];

    printf("Length of \"%s\": %zu\n", last, strlen(last));

    strcpy(full, first);       /* copy */
    strcat(full, " ");         /* append */
    strcat(full, last);
    printf("Full name: %s\n", full);

    if (strcmp(first, "Juan") == 0) {
        printf("Hi Juan!\n");
    }
    printf("strcmp(\"apple\", \"banana\") < 0? %d\n", strcmp("apple", "banana") < 0);
    return 0;
}

Output

Length of "Dela Cruz": 9
Full name: Juan Dela Cruz
Hi Juan!
strcmp("apple", "banana") < 0? 1
FunctionDoes
strlen(s)Number of characters (not counting '\0')
strcpy(dest, src)Copies src into dest
strcat(dest, src)Appends src to the end of dest
strcmp(a, b)0 if equal, negative if a comes first, positive if after
strncpy, strncatSafer versions with a maximum length
strchr(s, c), strstr(s, t)Find a character / substring

Changing case and checking characters

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

int main(void) {
    char word[] = "Pilipinas 2026";
    int letters = 0, digits = 0;
    for (size_t i = 0; i < strlen(word); i++) {
        if (isalpha((unsigned char) word[i])) letters++;
        if (isdigit((unsigned char) word[i])) digits++;
        word[i] = toupper((unsigned char) word[i]);
    }
    printf("%s\n", word);
    printf("Letters: %d, digits: %d\n", letters, digits);
    return 0;
}

Output

PILIPINAS 2026
Letters: 9, digits: 4

<ctype.h> has isalpha, isdigit, isspace, isupper, toupper and tolower. The (unsigned char) cast keeps them safe for all characters.

Reversing a string

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

int main(void) {
    char s[] = "Rizal";
    int n = strlen(s);
    for (int i = 0; i < n / 2; i++) {
        char t = s[i];
        s[i] = s[n - 1 - i];
        s[n - 1 - i] = t;
    }
    printf("%s\n", s);
    return 0;
}

Output

laziR

Palindrome check

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

int main(void) {
    const char *words[] = {"Anna", "Level", "Kayak", "Mabuhay"};
    for (int w = 0; w < 4; w++) {
        const char *s = words[w];
        int n = strlen(s), ok = 1;
        for (int i = 0; i < n / 2; i++) {
            if (tolower((unsigned char) s[i]) != tolower((unsigned char) s[n - 1 - i])) {
                ok = 0;
                break;
            }
        }
        printf("%-8s %s\n", s, ok ? "palindrome" : "not a palindrome");
    }
    return 0;
}

Output

Anna     palindrome
Level    palindrome
Kayak    palindrome
Mabuhay  not a palindrome

Reading strings safely

Use fgets(buffer, sizeof buffer, stdin) to read a line; it never writes past the end of your array. Avoid gets (removed from C because it's unsafe) and be careful with scanf("%s"), which reads one word and has no size limit unless you write one, like %19s for a 20-byte array.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How many bytes does the string "Kape" need?
2. What does strcmp(a, b) return when the strings are equal?
3. Which function joins one string onto another?
4. Why should you avoid gets?

By Sir Reyel · Last updated 2026-12-04