String manipulation part 2

Searching for a character inside a string (First occurrence)

The function which header is char* strchr(const char* str, int character) searches for the character given as second argument, inside the string given as first argument and returns a pointer to the character if it is found. It returns 0 otherwise. If there are, in the string, more than one instance of the character given as second argument, a pointer to the first one reached is returned.

Note that the function header shown above is the one defined in C. With a C++ compiler, those 2 versions (overloads) of the function are defined instead:

const char* strchr(const char* str, int character) char* strchr(char* str, int character)

The only difference is that if the first argument is a pointer to a constant char array, the returned pointer will point to a constant char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string.h> #include <stdio.h> int main() { char str[] = "No word iz misspelled in that sentence."; // Seeks the character 'z' and replaces it by 's'. *strchr(str, 'z') = 's'; // Now, what is printed in the console is true. puts(str); return 0; }

Searching for a character (last occurrence)

The function which header is char* strrchr(const char* str, int character) does the same as the function strchr, except that if the character given as last argument is present many times in the string, a pointer to the last one is returned.

The header of the function above is the one defined in C. In C++, the following versions (overloads) of the function are defined instead:

const char* strrchr(const char* str, int character) char* strrchr(char* str, int character)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string.h> #include <stdio.h> int main() { char str[] = "No word is misspelled in that sentencs."; // Seeks the last character 's' and replaces it by 'e'. *strrchr(str, 's') = 'e'; // Now, what is printed in the console is true. puts(str); return 0; }

Index of first character that is part of an other string

The function which header is size_t strcspn(const char* str, const char* str2) searches inside the string given as first argument for any character that is part of the one given as second parameter and returns the index inside the first string where the common character has been found. It also searches for the null character at the end of the strings, so if there is no character in common, the index returned is the index of the null-character at the end of the string in first argument, which is the size of the string (The null-character excluded).

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
#include <string.h> #include <stdio.h> int main() { char str[] = "I am a string."; char str2[] = "HELLO"; char charSet[] = "Abcdefg"; int index = strcspn(str, charSet); if(index == sizeof(str)-1) puts("No character in common."); else printf("The first character in common is %c.\n", str[index]); int index2 = strcspn(str2, charSet); if(index2 == sizeof(str2)-1) puts("No character in common."); else printf("The first character in common is %c.\n", str2[index2]); return 0; }

Pointer to the first character also in an other string

The function strpbrk, which header is char* strpbrk(const char* str, const char* str2), is used to look inside the first string given as argument to find any character that is also in the second string. If it finds one, it returns a pointer to it. Otherwise, it returns 0. Note that it stops at the first character, in common, found.

The header above is the one defined in C. With a C++ compiler, the 2 following versions (overloads) of the function are defined instead:

const char* strpbrk(const char* str, const char* str2) char* strpbrk(char* str, const char* str2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string.h> #include <stdio.h> int main() { char str[] ="all vowels are replaced by underscores."; char vowels[] = "aeiouy"; // Retrieves a pointer to the first letter in 'str' that is a vowel. char *ptr = strpbrk(str, vowels); while(ptr != 0) // As long as a vowel is found. { *ptr = '_'; // Retrieves a pointer to the first letter in 'str' that is a vowel. ptr = strpbrk(str, vowels); } // Prints the transformed string. puts(str); return 0; }

Number of character in common, in a row, from two strings

The function which header is size_t strspn(const char* str. const char* str2) returns the number of character, from the start of the first string received in argument, in a row, that is also contained in the second string. That means that if both strings are equal, the length of the strings is returned and if the first character of the strings are different, 0 is returned.

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[] ="aeiok"; char str2[] = "baioiueer"; char vowels[] = "aeiouy"; // Prints the number of vowels, from the start of 'str', until a consonant is reached. printf("The first string starts with %u vowels.\n", strspn(str, vowels)); // Prints the number of vowels, from the start of 'str2', until a consonant is reached. printf("The second string starts with %u vowels.\n", strspn(str2, vowels)); return 0; }

Searching for a string inside a string

The function strstr, which header is char* strstr(const char* str, const char* str2), searches inside the first string received in argument for the string received in second argument. If the string is found, a pointer to the start of it is returned. Otherwise, a null-pointer is returned.

Again, the function header above is the C version. In C++, the following versions (overloads) are defined:

const char* strstr(const char* str, const char* str2) char* strstr(char* str, const char* str2)
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 <stdio.h> int main() { char options[] = "blue red yellow orange green"; // If the string "orange" has been found inside the string contained by 'options'. if(strstr(options, "orange")) puts("\"orange\" is part of the options."); else puts("\"orange\" is not part of the options."); // If the string "purple" has been found inside the string contained by 'options'. if(strstr(options, "purple")) puts("\"purple\" is part of the options."); else puts("\"purple\" is not part of the options."); // Replaces the first letter of the string "red" inside 'options', by 'b'. strstr(options, "red")[0] = 'b'; // Outputs the string inside the array 'options'. puts(options); // Prints the options, starting from "yellow". puts(strstr(options, "yellow")); return 0; }

Splitting a string into elements

The function which header is char * strtok(char* str, const char* delimiters) is used to separate the string received as first argument into sub-strings delimited by any of the characters inside the second string received as parameter. The function searches inside the first string for one of the delimiters (One of the characters inside the second string). If one is found, it is replaced by a null character and a pointer to the start of the sub-string located before the delimiter is returned. If there are many delimiters in a row, the additional delimiters are skipped. We must use it as follow: We give it the string we want to parse through as first argument the first time we call the function. Then, by giving a null pointer as first argument for the following calls, it will automatically continue from where the last call stopped. If the string given as argument starts with delimiters, they are skipped. When there are no element left to read (The internal pointer of the function strtok points to the end of the string), it returns a null pointer.

Here is an example where we retrieve each word inside a string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string.h> #include <stdio.h> int main() { char sentence[] = "This sentence will be separated into words."; puts(sentence); char *word = strtok(sentence, " "); // As long as 'word' does not point to the address 0. while(word) { puts(word); word = strtok(0, " "); } return 0; }

Here is an example where we read the elements of a variable definition:

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
30
31
32
33
34
35
36
37
38
39
#include <string.h> #include <stdio.h> int main() { char code[] = "int var = 0;"; char delims[] = " =;"; // Reads next element (Sub-string). char* elem = strtok(code, delims); // If an element (Sub-string) has been found. if(elem != 0) { printf("Variable type: %s.\n", elem); // Reads the next element (Sub-string). elem = strtok(0, delims); // If an element (Sub-string) has been found. if(elem != 0) { printf("Variable name: %s.\n", elem); // Reads next element (Sub-string). elem = strtok(0, delims); // If an element (Sub-string) has been found. if(elem != 0) printf("Value of the variable: %s.\n", elem); } } elem = strtok(0, delims); if(elem == 0) puts("All the elements (inside 'code'), separated by the delimiters, has been retrieved."); return 0; }