Standard library string functions (strcpy, strcat, strlen, etc.).

Lesson 5.5: Standard Library String Functions


C doesn't have a built-in string data type like some other languages (e.g., Python or Java). Instead, strings are represented as arrays of characters terminated by a null character (\0).  Fortunately, the C standard library (<string.h>) provides a rich set of functions to make working with strings easier. Let's explore some of the most common ones:


1. strcpy(destination, source)


Purpose: Copies the contents of the source string (including the null terminator) to the destination string.

Important: Ensure the destination array is large enough to hold the source string.

Example:

C

#include <stdio.h>

#include <string.h>


int main() {

    char src[] = "Hello";

    char dest[10]; // Sufficient space to hold "Hello\0"


    strcpy(dest, src);

    printf("%s\n", dest); // Output: Hello

    return 0;

}


2. strcat(destination, source)


Purpose: Appends (concatenates) the source string to the end of the destination string. The null terminator of destination is replaced with the first character of source.

Important: Ensure the destination array has enough remaining space to hold the source string and the null terminator.

Example:

C

char str1[20] = "Hello";

char str2[] = " World";

strcat(str1, str2);

printf("%s\n", str1); // Output: Hello World


3. strlen(string)


Purpose: Returns the length of a string (the number of characters before the null terminator).

C

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

int length = strlen(message);

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


4. strcmp(string1, string2)


Purpose: Compares two strings lexicographically (based on character values).

Return Value:

0: If the strings are identical.

Negative: If string1 is less than string2.

Positive: If string1 is greater than string2.

Example:

C

if (strcmp("apple", "banana") < 0) {

    printf("apple comes before banana\n");

}


5. strncmp(string1, string2, n)


Purpose: Compares the first n characters of two strings. Useful when you want to compare only parts of strings.

Additional Functions


strchr(string, character): Finds the first occurrence of a character in a string.

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

memcpy(destination, source, n): Copies n bytes from the source to the destination (can be used for copying parts of strings).

memset(string, character, n): Sets the first n bytes of a string to the specified character.

Important Considerations


String Immutability: String literals in C are typically stored in read-only memory. Attempting to modify them directly might lead to errors.

Buffer Overflows: Be extremely careful not to overflow the destination buffers when using functions like strcpy and strcat. Always ensure you have allocated enough space.

Hands-On Exercise


Write a program that gets two names from the user.

Compare the names to determine which one comes first alphabetically.

Concatenate the names and print the combined string.

Course Syllabus