Hello, I’m Liang Tang.

This is part 11 of EasyC++ series. We will talk about common functions in cstring.h.

For a better reading experience, visit the Github repository

Cstring. h Common functions

The cstring.h library is the C language string.h library, which is a standard library for strings in C language. C++ is compatible with this, so we can use it in C++.

There are a number of string manipulation apis in this library, and this article has selected some of the most common ones.

strlen

Since the compiler determines the end of the string by the position \0, the length of the string is not equal to the length of the array. We can use the strlen function to find the true length of the string:

char name[100] = "hello world";
cout << strlen(name) << endl;
Copy the code

For example, we use a char array of length 100 to store the “HelloWorld” string, which is only 11 when we use strlen.

strcat

The strcat function concatenates two strings. Its function signature is:

char *strcat(char *dest, const char *src)
Copy the code

We can see that it takes two arguments, dest and SRC, both of type char*, and returns a char* result. In C++, an array name is a constant pointer to the first element in the array. So even though the parameters in the signature are Pointers, we can pass in the array name as well.

After we pass two strings, the strcat function concatenates the SRC string at the end of the dest string and returns a pointer to the concatenated result. So you get the same value in the following two ways.

char name[100] = "hello world";
char level[100] = "concat test";

char *ret = strcat(name, level);
cout << ret << endl;	1 / / way
cout << name << endl;	2 / / way
Copy the code

strncat

A variant of the strcat function that takes an extra parameter to control the maximum length of the SRC string to be copied.

char *strncat(char *dest, const char *src, size_t n)
Copy the code

Let’s use the same example:

char name[100] = "hello world";
char level[100] = "concat test";

char *ret = strncat(name, level, 4);
cout << ret << endl;
cout << name << endl;
Copy the code

Since we pass in 4, limiting the length of the level string copy, we end up with hello worldconc.

strcpy

String copy function that copies the contents of the SRC string to dest.

char *strcpy(char *dest, const char *src)
Copy the code

The usage is similar to the other functions described earlier, with two caveats.

  1. ifdestThe string length is not long enough to report errors at compile time, but can cause problems at run time.
char name[10] = "";
char level[100] = "concat test";

strcpy(name, level);
cout << name << endl;
Copy the code

The above code will compile and run, but the terminal will display an error message when running.

So be careful when using Strcpy and make sure dest has sufficient length.

  1. ifdestThe content is already there and will be overwritten.
char name[15] = "abc";
char level[100] = "concat test";

strcpy(name, level);
cout << name << endl;
Copy the code

After strcpy is run, the name is cleared.

strncpy

Strcpy adds a length – limited version of strcpy. An additional argument n may be passed to indicate that a maximum of n characters can be assigned. When the SRC length is less than n, the rest will be filled with empty bytes.

char *strncpy(char *dest, const char *src, size_t n)
Copy the code
char name[15] = "abc";
char level[100] = "concat test";

strncpy(name, level, 4);
cout << name << endl;
Copy the code

The output is CONC.

memset

String batch setting function, you can set the string batch to a character.

void *memset(void *str, int c, size_t n)
Copy the code

Int c indicates the character to be set, and size_t n indicates the number of characters to be set.

char name[15] = "abc";
char level[100] = "concat test";

memset(name, 'c'.10);
cout << name << endl;
Copy the code

The result of running the code above is 10 cs.

In addition, memsets can be used to batch set strings as well as arrays of type ints. Since a 32-bit int takes up four bytes, it is four characters long. So when using memset for batch setting, the final result will be the concatenation of four incoming int c’s.

int a[100];
memset(a, 1.sizeof a); // memset(a, 1, 400); Because an int takes up four bytes
Copy the code

When we set this up, instead of 1, the element in the array A is 0x01010101, which in base 10 is 16843009. So memset initializes an int array with only three operations:

// 1. Batch set to 0
memset(a, 0.sizeof a);
// 2. Batch set to -1
memset(a, - 1.sizeof a);
// 3. Batch set to a value close to the upper limit of the maximum integer
memset(a, 0x7f.sizeof a);
memset(a, 0x3f.sizeof a);
Copy the code

Some of the specifics of memset usage will be covered in practical problems that follow.

memcpy

Similar to strcpy, copy N characters from STR2 to str1.

void *memcpy(void *str1, const void *str2, size_t n)
Copy the code
char name[15] = "abc";
char level[100] = "concat test";

memcpy(name, level, 10);
Copy the code

strcmp

Compare two strings in lexicographical order, defined as left-to-right character by character (by ASCII value size) until a different character or occurrence of \0 occurs.

int strcmp(const char *str1, const char *str2)
Copy the code

The result returned is an int. If it is less than 0, str1 is less than str2, if it is equal to 0, str1 is greater than str2.

char name[15] = "abc";
char level[100] = "abcd";

cout << strcmp(name, level) << endl;
Copy the code

If the command output is -100, name is smaller than level.

strncmp

STRCMP length limited version, can accept an additional number n, indicating that at most characters of length N can be compared.

int strncmp(const char *str1, const char *str2, size_t n)
Copy the code

strstr

char *strstr(const char *haystack, const char *needle)
Copy the code

Search haystack for the position where needle first appeared and return the pointer to that position.

char name[15] = "search-test";
char level[100] = "-";

char *ret = strstr(name, level);
cout << ret << endl;
Copy the code

After running, the screen output is as follows:

Because when we print a char * variable using cout, it will be printed as a string, that is, until we hit \0.

The output is -test, indicating that we have used the STRSTR function to find the occurrence of the “-“, skipping the previous content.

In addition to the functions introduced above, there are many other APIS in Cstring, such as strtok,memmove and so on. If you are interested, please go to the relevant documentation, you may be surprised.