String manipulation part 1

The header <string.h>

The header <string.h> declares functions used to manipulate arrays. Some of them expect the arrays in argument to represent null-terminated strings and others do not. This page presents some of the functions who works on strings and the other ones (Except 2 of them) are presented in the next page. The page after the next page presents the other functions which work on any kind of array.

Copying a string

The function strcpy, which header is char* strcpy(char* dest, const char* src), is used to copy the null-terminated string given as second argument into the character array received as first argument. It returns back the first argument. The array given as first argument must be big enough to hold the null-terminated string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string.h> #include <stdio.h> #include <stdlib.h> int main() { char str[] = "I am a string."; // Allocates enough memory to contain the string inside 'str'. char *copy = (char*)malloc(sizeof(str)); // Copies the string from 'str' to 'copy'. strcpy(copy, str); // Prints the copied string. puts(copy); // Frees the dynamically allocated memory. free(copy); return 0; }

Copying part of a string

The function which header is char* strncpy(char* dest, const char* src, size_t num) copies the string pointed by the second argument into the first argument until the number of character specified by the last argument is written. In case the number given as last argument is bigger than the size of the null-terminated string given in second argument, zeros are written to the array in first argument until the number of character specified is written. In case the number given in last argument is smaller than the string in second argument, no zero (Null-character) is added to the array of destination (Therefore, it is not made a null-terminated string). The function returns back the first argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string.h> #include <stdio.h> int main() { char str[] = "I am a string."; char copy[6]; // Copies the first 4 characters of the string inside 'str' to the array 'copy'. strncpy(copy, str, 4); /* The array 'copy' contains the string "I am". There is however no zero at the end of it (It is not a null-terminated string), so if we try to output it now, the program would probably crash, because the outputting function would continue writing characters even after the end of the string, since it would not reach the null character. */ // We manually add a point to the string. copy[4] = '.'; // We manually add a null-character, making it a null-terminated string. copy[5] = 0; // Prints the copied string. We can do it because it is a null-terminated string. puts(copy); return 0; }

Adding a string after a string

The function strcat, which header is char* strcat(char* dest, const char* src), is used to copy the null-terminated string given in second argument to the end of the null-terminated string in first argument, overwriting its null character. Returns the first argument back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string.h> #include <stdio.h> int main() { char str[13] = "How "; char str2[] = "are "; // Copies the string inside 'str2' at the end of 'str'. strcat(str, str2); // Copies the string "you?" at the end of 'str'. strcat(str, "you?"); // Prints the conCATenation (strCAT) of the 3 strings. puts(str); return 0; }

Adding part of a string after a string

The function which header is char* strncat(char* dest, const char* src, size_t num) copies the string given in second argument to the end of the string in first argument, overwriting its null character, until the number of characters specified in last argument has been written. If the number given in last argument is bigger than the size of the array in second argument, it simply writes the whole string, nothing more. Even if not the whole string is written, a null character is added at the end of the array in first argument, making it a null-terminated string. It returns the first argument back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string.h> #include <stdio.h> int main() { char str[30] = "My name is "; char names[] = "Jack Elliot Tommy Williams"; // Copies the string "Elliot" at the end of the string inside 'str'. strncat(str, &names[5], 6); // Prints the resulting string. puts(str); return 0; }

Comparing two strings

The function strcmp, which header is int strcmp(const char* str, const char* str2), compares the two null-terminated strings received in argument and returns 0 if they are both equal. Otherwise, the returned number is a negative if the first character that is different has a lower value in the first string given in argument than in the second one and positive if it is the other way around.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string.h> #include <stdio.h> int main() { char str[] = "Blue", str2[] = "Yellow"; if(strcmp(str, str2) == 0) puts("Both strings are equal."); else puts("Both strings are not equal."); if(strcmp("Yellow", str2) == 0) puts("Both strings are equal."); else puts("Both strings are not equal."); return 0; }

Comparing a part of two strings

The function which header is strncmp(const char* str, const char* str2, size_t num) does the same as the function strcmp, except that the number given as last argument represents the maximum amount of character compared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string.h> #include <stdio.h> int main() { char str[] = "Blue", str2[] = "Blueish"; if(strncmp(str, str2, 4) == 0) puts("Both parts of the strings are equal."); else puts("Both parts of the strings are not equal."); return 0; }

Retrieving the length of a string

The function which header is size_t strlen(const char* str) counts the characters inside the string received in argument until it reaches the null-character (It does not count the null-character). It then returns the number of character counted (The length of the string).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string.h> #include <stdio.h> int main() { char str[50] = "How long am I?"; unsigned length = strlen(str); printf("The length of str is %u.\n", length); /* "The length of str is 14.\n" is printed into the console. */ return 0; }

Retrieving an error message from an error code

The function strerror, which header is char* strerror(int errnum), is used to return a pointer to a null-terminated string describing the error code received in argument. The returned string must not be modified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string.h> #include <errno.h> #include <stdio.h> #include <math.h> int main() { // errno equals 0 (No error). puts(strerror(errno)); /* The function which header is double sqrt(double n), declared in the header <math.h>, calculates and returns the square root of its argument */ // Oups. The square root of a negative number does not return a real number. float n = sqrt(-1); // The last operation set errno to an error code. Prints the error message of that error code. puts(strerror(errno)); /* The output may be "Domain error". That means that the last operation received an argument for which its result is not defined (Not part of its domain of arguments). */ // Sets errno back to 0. errno = 0; puts(strerror(errno)); return 0; }