Character arrays and strings (null-terminated strings).

Lesson 5.4: Character Arrays and Strings (Null-Terminated Strings)


In this lesson, you'll learn how to work with text in C using character arrays and a special kind of array called a null-terminated string. Strings are essential for storing and manipulating text data, such as names, messages, and file paths.


Character Arrays


Arrays of Characters: A character array is simply an array where each element stores a single character (char).

Declaration:

C

char myChars[10]; // An array to hold 10 characters

Initialization:

C

char myChars[5] = {'H', 'e', 'l', 'l', 'o'};


Important: Character arrays are not automatically strings. They become strings when terminated with a null character (\0).

Null-Terminated Strings


What Are They? A null-terminated string is a character array that ends with a null character (\0). The null character acts as a marker to indicate the end of the string.

Why Null-Terminated? C uses null-terminated strings because they're simple and efficient to work with. Many C string manipulation functions rely on the presence of the null terminator.

String Literals: When you write text enclosed in double quotes (e.g., "Hello"), C automatically adds the null terminator.

C

char greeting[] = "Hello"; // This is a null-terminated string

// It is equivalent to: {'H', 'e', 'l', 'l', 'o', '\0'}

Accessing String Characters


You can access individual characters in a string using array indexing (starting from 0):


C

printf("%c\n", greeting[0]); // Output: H


Important Note: Do not forget to allocate an extra space for the null terminator when declaring character arrays to hold strings.


String Length


The strlen() function from the <string.h> header file returns the length of a string (excluding the null terminator).

C

#include <stdio.h>

#include <string.h>


int main() {

    char message[] = "Hello, world!";

    int length = strlen(message); 

    printf("Length of the string: %d\n", length); // Output: 13

    return 0;

}

Common String Functions


The C standard library (<string.h>) provides a rich set of functions for working with strings:


strcpy(): Copies one string to another.

strcat(): Appends one string to the end of another.

strcmp(): Compares two strings lexicographically.

strlen(): Returns the length of a string.

strstr(): Finds the first occurrence of a substring within a string.

And many more!

Example: String Manipulation


C

#include <stdio.h>

#include <string.h>


int main() {

    char str1[] = "Hello";

    char str2[20]; // Make sure str2 is large enough to hold the concatenated string


    strcpy(str2, str1);        // Copy str1 to str2

    strcat(str2, ", world!");   // Append ", world!" to str2


    printf("%s\n", str2);       // Output: Hello, world!


    return 0;

}

Key Points


Character Arrays: Store individual characters.

Null-Terminated Strings: Character arrays ending with '\0'.

String Functions: Use library functions for common string operations.

Memory Allocation: Allocate enough space for the string and its null terminator.

Let me know if you need help with more concepts or examples!

Course Syllabus