(C language) common character functions and string functions

Hits: 0

We all know that there is a header file string.h in C language, but there is no string type in C language.
[Strings] are usually placed in constant strings or character arrays. String constants are suitable for string functions that do not modify them.
The function prototypes declared in the header file string.h are also all for various operations on char [arrays .]
The string class did not appear until C++

This article will briefly sort out the [library functions] that deal with characters and strings in the C language .

strlen

size_t  strlen( const char*  str)

Function: Calculate the length of the string, excluding ‘\0’
Return value: Return the number of characters in the string
Description :

  • The strlen() function calculates the actual length of the string, and ends with the first ‘\0’;
  • The string pointed to by the parameter must end with ‘\0’
  • The return value of the function must be size_t, which is unsigned
  • If you only define and do not assign an initial value to it, the result is uncertain, it will search from the first address until it encounters ‘\0’ and stops
  • sizeof returns the amount of memory occupied by the variable after the declaration, not the actual length. In addition, sizeof is not a function, it is just an operator, and strlen() is a function

strcpy

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

Function: Copy the parameter src string to the address pointed to by the parameter dest
Return value: Return the string starting address of the parameter dest
Description:

  • Source string must end with ‘\0’
  • will copy the ‘\0’ of the source string to the destination space
  • The target space must be variable
  • If the memory space pointed to by the parameter dest is not large enough, it may cause a buffer overflow error. You need to pay special attention when writing the program, or use strncpy() instead.

strncpy

char* strncpy(char* dest,const char* src,size_t num)

Function: Copy the first num characters of the src string to dest
Return value: The starting address of the dest string
Description :

  • If the length of the src string is less than num, after copying the string, append 0 to the target until num
  • strncpy doesn’t append’\0′ to dest
  • The memory areas pointed to by src and dest cannot overlap, and dest must have enough space to place n characters

screwed up

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

Function: String concatenation
Return value: Return the starting address of the dest string
Description:

  • The source string must end with ‘\0’
  • The destination space must be modifiable
  • strcat() will copy the parameter src string to the end of the string pointed to by the parameter dest
  • The last end character ‘\0’ of dest will be overwritten , and add another ‘\0’ at the end of the concatenated string
  • the memory space pointed to by dest and src cannot overlap, and dest must have enough space to accommodate the string to be copied

strncat

char* strncat (char* dest,const char* src,size_t num)

Function: Append n characters to the end of the string
Return value: Return the starting address of the dest string
Description:

  • strncat will copy n characters from the beginning of the string src to the end of the dest string
  • dest must have enough space to hold the string to be copied
  • If n is greater than the length of the string src, only append all of src to the end of dest
  • strncat will overwrite the last ‘\0’ of the dest string, and then append ‘\0’ after the characters are appended

strcmp

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

Function: String comparison
Return value: If the strings of parameters s1 and s2 are the same, return 0, if s1 is greater than s2, return a value greater than 0, if s1 is less than s2, return a value less than 0
Explanation:

  • Determine the size of two strings 1) ASIC code 2) length
  • Case-sensitive comparison, if you want case-insensitive string comparison, you can use the stricmp function

strncmp

int strncmp(const char* str1,const char* str2,size_t num)

Function: specified length comparison
Return value: same as strcmp

strstr

char strstr(const char str,const char* substr)
Function: Retrieve the position of the first occurrence of the substring in the string
Return value: Return the address of the first occurrence of the substring substr in the string str; if no substring is retrieved string, return NULL

strchr

char* strchr(const char* str,char c)

Function: Find the position of the first occurrence of c in the string
Return value: Return the pointer to the position of the first occurrence of c, or NULL if c does not exist in s

strrchr

char* strchr(const char* str,char c)

Function: Find the position of the last occurrence of c in the string
Return value: Return the pointer to the position of the last occurrence of c, or NULL if c does not exist in s

strspn

size_t  strspn(const char* str1, const char* str2)

Function: used to calculate the number of characters at the beginning of the string str1 that matches the string of str2
Return value: Returns the number of characters that are consecutively included in the string str2 at the beginning of the string str1 .

  • If all the characters contained in str belong to str2, return the length of str1
  • Returns 0 if the first character of str does not belong to str2

strcspn

size_t  strspn(const char* str1, const char* str2)

Function: It is used to calculate the number of characters in the beginning part of the string str1 that do not match the string str2.
Return in the beginning part of the string str1 that do not appear in the string str2 .

strtok

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

Function: Divide the string into segments according to the delimiter
Return value: Return the pointer to the next segmented string, or NULL if there is no way to segment it
Explanation:
– The sep parameter is a string that defines the character used as the delimiter Set
– The first parameter specifies a string, which contains one or more tokens separated by one or more characters in the sqp string
– the first call will pass the first address of the string, and then the call will not pass the address , there will be a static function inside to save the pointer address
– the delimiter is not used as the output content, only the delimiter
– when strtok finds the separation character of the parameter sep in the string of the parameter s, it will change the character to ‘\0’ Characters
– strtok must assign the parameter str string in the first call, and set the parameter s to NULL for subsequent calls
– strtok will modify the original string, so it must be placed on the stack

strerror

char* strerror(int errnum)

Function: Returns a pointer to the error message string
Description :

  • The header file errno.h must be included

strpbrk

char* strpbrk(const char*  str1, const char* str2)

Function: Find the position of the first character in the string s1 that matches any character in the string s2, the null character NULL is not included

trailer

int atoi(const char* str)

Function: Convert string to integer
Return value: Return the converted integer. If str cannot be converted to int or str is an empty string, then 0 will be returned to
indicate:

  • The ANSI C specification defines 6 functions, stof(), atoi(), atol(), strtod(), strtol(), and strtoul(), that can convert strings to numbers, which can be compared and learned
  • In addition, 5 new functions have been added to the C99/C++11 specification, namely atoll(), strtof(), strtold(), strtoll(), strtoull()

Character classification function:

int isalnum( int c): Check whether the character is a number or letter; ( 0 ~ 9 ,a~z,A~Z) 
 int isalpha( int c): Check whether the character is a letter; (a~z, A~Z) 
 int iscntrl( int c): check if the character is a control character; (octal 000 ~ 037 and 177 characters) 
 int isdigit( int c): check if the character is a decimal number; ( 0 ~ 9 ) 
 int isgraph( int c): Check whether the character is a graphical representation, depending on the language environment; 0 ~ 9 , a~z, A~Z, and punctuation) 
 int islower( int c): check whether the character is a lowercase letter; (a~z) 
 intisprint( int c): Check if the character is printable; (numbers, letters, punctuation, whitespace) 
 int ispunct( int c): Check if the character is punctuation; (! ” # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~ etc.) 
int isspace( int c): check if the character is whitespace; (TAB, line feed, vertical TAB, form feed, carriage return , space) 
 int isupper( int c): Check if the character is uppercase; (A~Z) 
 int isxdigit( int c): Check if the character is a hexadecimal digit; ( 0  1  2  3  4  5  6  7  8  9 ABCDEF abcdef)

character conversion

int  tolower ( int c ): Convert characters to lowercase letters; 
 int  toupper ( int c ): Convert characters to uppercase letters;

You may also like...

Leave a Reply

Your email address will not be published.