C and pointer learning notes -- string common library functions

Posted by maga2307 on Thu, 07 Oct 2021 22:47:46 +0200

This series is mainly some notes on my study of C and pointer. It is mainly about some small details for my own study and reference. In detail, it is suggested that you can read some of C and pointer

String length

The prototype of the library function strlen is as follows:

size_t strlen( char const *string );

Note that strlen returns a similar to size_ The value of T. This type is defined in the header file stddef.h, which is an unsigned integer type. Using unsigned numbers in expressions can lead to unpredictable results. For example, the following two expressions look equal:

if( strlen( x ) >= strlen( y ) ) ...
if( strlen( x ) - strlen( y ) >= 0 ) ...

But in fact, they are not equal. The first statement will work as you expected, but the result of the second statement will always be true. The result of strlen is an unsigned number, so the expression on the left of the operator > = will also be an unsigned number, and the unsigned number can never be negative.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char s[] = "123456";
    printf("%d\n", strlen(s)); //Output 6
    return 0;
}

Unrestricted string function

The most commonly used strings are "unrestricted", that is, they only judge their length by looking for the NUL byte at the end of the string parameter. These functions generally specify a piece of memory to store the result string. When using these functions, the programmer must ensure that the result string does not overflow this memory.

Copy string

The function used to copy strings is strcpy. Its prototype is as follows:

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

This function copies the parameter src string to the dst parameter and returns a pointer to the target character array. If the parameters src and dst overlap in memory, the result is undefined. Since the dst parameter will be modified, it must be a character array or a pointer to an array of dynamically allocated memory. String constants cannot be used.
Note that the target character array has enough space to hold the string to be copied. Otherwise, the value of the memory space behind the array will be overwritten.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char s1[] = "123456";
    char s2[] = "890";
    strcpy(s1, s2);
    int i;
    for(i=0; s1[i]; ++i)
        printf("%c", s1[i]); //Output 890
    return 0;
}

Connection string

To add (concatenate) one string after another, use the strcat function. Its prototype is as follows:

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

The strcat function requires that the dst parameter already contains a string (which can be an empty string). It finds the end of the string, adds a copy of the src string to this location, and returns a pointer to the target character array. If the positions of src and dst overlap, the result is undefined.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char s1[100] = "123456";
    char s2[] = "890";
    strcat(s1, s2);
    for(int i=0; s1[i]; ++i)
        printf("%c", s1[i]); //Output 1234567890
    return 0;
}

string comparison

The library function strcmp is used to compare two strings. Its prototype is as follows:

int strcmp( char const *s1, char const *s2 );

If s1 is less than s2, the strcmp function returns a value less than zero; If s1 is greater than s2, the function returns a value greater than zero; If the two strings are equal, the function returns zero.
Note that this is a dictionary comparison, and equality outputs 0 instead of 1
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char s1[100] = "123456";
    char s2[] = "890";
    printf("%d\n", strcmp(s1, s2)); //Output - 1
    return 0;
}

Length limited string function

The standard library also contains functions that handle strings in a different way. These functions accept an explicit length parameter that limits the number of characters to copy or compare. These functions provide a convenient mechanism to prevent unpredictable long strings from overflowing from their target array.
The prototypes of these functions are as follows:

char *strncpy( char *dst, char const *src, size_t len );
char *strncat( char *dst, char const *src, size_t len );
int strncmp( char const *s1, char const *s2, size_t len );

Like strcpy, strncpy copies the characters of the source string to the target array. However, it always writes exactly len characters to the dst. If the value of strlen (STC) is less than len, the dst array is filled with additional NUL bytes to the length of len; If the value of strlen (SRC) is greater than or equal to len, only len characters are copied into dst. be careful! Its result will not end in NUL bytes.

String lookup function

Find a character

The easiest way to find a specific character in a string is to use the strchr and strrchr functions. Their prototypes are as follows:

char *strchr( char const *str, int ch );
char *strrchr( char const *str, int ch );

Note that their second parameter is an integer value. However, it contains a character value. Strchr finds the position where the character ch first appears in the string str. after finding it, the function returns a pointer to the position. If the character does not exist in the string, the function returns a NULL pointer. The function of strrchr is basically the same as that of strchr, except that it returns a position pointing to the last occurrence of the character in the string (the rightmost one).
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char string[20] = "Hello there, honey.";
    char *ans;

    ans = strchr( string, 'h');
    while(*ans)
        printf("%c", *ans++); //Output here, honey
    return 0;
}

Find any few characters

strpbrk is a more common function. Instead of looking for a particular character, it looks for the first position of any group of characters in the string. Its prototype is as follows:

char *strpbrk( char const *str, char const *group );

This function returns a character position that points to any character in the first matching group in str. If no match is found, the function returns a NULL pointer.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char string[20] = "Hello there, honey.";
    char *ans;

    ans = strpbrk( string, "aeiou");
    while(*ans)
        printf("%c", *ans++); //Output ello there, honey
    return 0;
}

Find a substring

To find a substring in a string, you can use the strstr function. Its prototype is as follows:

char *strstr( char const *s1, char const *s2 ); 

This function finds the starting position of the first occurrence of the whole s2 in s1 and returns a pointer to that position. If s2 does not appear completely anywhere in s1, the function returns a NULL pointer. If the second argument is an empty string, the function returns s1.

Find a string prefix

The strspn and strcspn functions are used to count characters at the beginning of a string. Their prototypes are as follows:

size_t strspn( char const *str, char const *group );
size_t strcspn( char const *str, char const *group );

The group string specifies one or more characters. strspn returns the number of characters in which the beginning of str matches any character in the group.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    int len1, len2;
    char buffer[] = "25,142,330,Smith,J,239-4123";
    len1 = strspn( buffer, "0123456789" ); // 2
    len2 = strspn( buffer, ",0123456789"); // 11
    printf("%d %d\n", len1, len2 ); // Output 2 11
    return 0;
}

Find tag

The strtok function isolates individual parts of a string called token s and discards the delimiters. Its prototype is as follows:

char *strtok( char *str, char const *sep );

The sep parameter is a string that defines the set of characters used as delimiters. The first parameter specifies a string that contains zero or more tags separated by one or more separators in the sep string. strtok finds the next tag of str, ends it with NUL, and returns a pointer to this tag.
A chestnut

#include <stdio.h>
#include <string.h>
int main()
{
    char whitespace[] = " \t\f\r\v\n";
    char *token;
    char line[] = "Hello, nice to meet you.";
    for( token = strtok( line, whitespace );
        token != NULL;
        token = strtok( NULL, whitespace))
        printf("Next token is %s\n", token );
    return 0;
}

Topics: C C++ string