Course Content
C Tutorial
About Lesson

String Manipulation Functions (strcpy, strcat, strlen, etc.)

C programming language offers various string manipulation functions that allow developers to work efficiently with strings. These functions, including strcpy, strcat, strlen, and others, are fundamental for handling character arrays and strings in C.

strcpy – String Copy

The strcpy function is used to copy the contents of one string into another. Its syntax is straightforward:

char *strcpy(char *dest, const char *src);

For instance:

char source[] = "Hello";
char destination[20];
strcpy(destination, source);

This copies the content of source to destination.

strcat – String Concatenation

The strcat function concatenates two strings, appending the content of the source string to the end of the destination string. Its syntax is:

char *strcat(char *dest, const char *src);

Example:

char dest[20] = "Hello";
char src[] = " World!";
strcat(dest, src);

This results in dest containing “Hello World!”.

strlen – String Length

The strlen function computes the length of a given string, excluding the null character . Its syntax is simple:

size_t strlen(const char *str);

For instance:

char str[] = "Hello";
size_t length = strlen(str);

Here, length will be 5, as there are 5 characters in “Hello”.

strcmp – String Comparison

The strcmp function compares two strings lexicographically. It returns an integer value based on the comparison:

int strcmp(const char *str1, const char *str2);

For example:

char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);

The result will be negative, zero, or positive depending on whether str1 is less than, equal to, or greater than str2, respectively