C Language: Character Functions and String Functions (A good article on string functions!)

Table of contents

Find the length of a string:

1. strlen (string length)

Functions of unlimited length:

2. strcpy (string copy)

3. strcat (string append)

4. strcmp (string comparison)

Length-restricted functions:

5. strncpy (string copy)

6. strncat (string append)

7. strncmp (string comparison)

String lookup:

8. strstr (find string substring)

9. strtok (string split)

Error message report:

10. strerror (return error message)

Character manipulation functions:

Character conversion:

1. tolower (lowercase -> uppercase)

2. toupper (uppercase -> lowercase)

Memory manipulation function:

1. memcpy (memory copy)

2. memmove (memory copy)

3. memcmp (memory comparison)

4. memset (memory settings )


Find the length of a string:

1. strlen (string length)

size_t   strlen ( const char * str );
        str: C string.
Return value: unsigned int.
1.1 The string has '\0' as the end mark , and the strlen function returns the number of characters that appear before '\0' in the string (not including
contains '\0' ).
1.2 The string pointed to by the parameter must end with '\0' .
1.3 Note that the return value of the function is size_t , which is unsigned ( error-prone).
#include <stdio.h>
#include <string.h>

int main()
{
	if ((int)strlen("abc") - (int)strlen("abcdef") > 0)
	{
		printf("大于\n");
	}
	else
	{
		printf("小于等于\n");
	}

	return 0;
}

Functions of unlimited length:

2. strcpy (string copy)

char*  strcpy(char * destination, const char * source );
        destinationob: Pointer to the destination array where the contents are to be copied .
        source: The C string to copy.
2.1 The source string must end with '\0' .
2.2 will copy the '\0' in the source string to the target space.
2.3 The target space must be large enough to ensure that the source string can be stored.
2.4 The target space must be variable.
#include <stdio.h>
#include <string.h>

int main()
{
	//char arr1[3] = "";
	//char arr2[] = "hello bit";
	char* arr1 = "xxxxxxxxxx";
	char arr2[6] = { 'a', 'b', 'c', 'd', 'e' , '\0'};
	strcpy(arr1, arr2);
	printf("%s\n", arr1);

	return 0;
}

3. strcat (string append)

char * strcat ( char * destination, const char * source );
        
         destination: A pointer to a destination array that should contain C strings and be large enough to contain the resulting strings of the concatenation.
        source: The C string to append. This should not overlap the target .
3.1 The source string must end with '\0' .
3.2 The target space must be large enough to accommodate the contents of the source string.
3.3 The target space must be modifiable.
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "world";
	strcat(arr1, arr2);

	printf("%s\n", arr1);
	return 0;
}

4. strcmp (string comparison)

int strcmp ( const char * str1, const char * str2 );
        
         str1: The C1 string to compare.
         str2 : The C2 string to compare.
4.1 The standard stipulates:
        4.1.1 If the first string is greater than the second string, a number greater than 0 will be returned ;
        4.1.2 If the first string is equal to the second string, then return 0 ;
        4.1.3 If the first character string is less than the second character string, a number less than 0 will be returned .
#include <stdio.h>
#include <string.h>

int main()
{
	int ret = strcmp("bbq", "bcq");
	if (ret>0)
		printf(">\n");

	printf("%d\n", ret);
	return 0;
}

Length-restricted functions:

5. strncpy (string copy)

char * strncpy ( char * destination, const char * source, size_t num );
        
         destination: Pointer to the destination array where the contents are to be copied.
         source: The C string to copy.
         num : the maximum number of characters to copy from the source ;

                  size_t  is an unsigned integer type.

5.1 Copies num characters from the source string to the target space .
5.2 If the length of the source string is less than num , after copying the source string, add 0 to the end of the target until num .
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "xxx";
	strncpy(arr1, arr2, 5);

	return 0;
}

6. strncat (string append)

char * strncat ( char * destination, const char * source, size_t num );
        
         destination: A pointer to a destination array that should contain a C string and be large enough to contain the resulting string of concatenation, including additional null characters.
        source: The C string to append.
         num: The maximum number of characters to append.

                 size_t is an unsigned integer type.

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "abcdef\0yyyyyyyy";
	char arr2[] = "xxxxxxxxx";
	strncat(arr1, arr2, 3);

	return 0;
}

7. strncmp (string comparison)

int strncmp ( const char * str1, const char * str2, size_t num );
        
         str1: The C1 string to compare.
        str2: The C2 string to compare.
       num: The maximum number of characters to compare.
                 size_t is an unsigned integer type.
7.1 Compare until another character is different or a string ends or all num characters are compared .

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcqwertyuiop";
	char arr2[] = "abcdef";
	printf("%d\n", strncmp(arr1, arr2, 4));

	return 0;
}

String lookup:

8. strstr (find string substring)

char * strstr ( const char *str1, const char * str2);
        
         str1: C string to scan.
         str2: C string containing the sequence of characters to match.
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abbbcdef";
	char arr2[] = "bbc";

	char* ret = strstr(arr1, arr2);
	if (ret != NULL)
		printf("%s\n", ret);
	else
		printf("找不到\n");

	return 0;
}

9. strtok (string split)

char * strtok ( char * str , const char * sep );
        
         str: C string to truncate. Note that this string is modified by breaking it into smaller strings (tokens). Alternatively, a null pointer can be specified, in which case the function will continue scanning the location of previous successful function calls.
        sep : A C string containing the separator character. These may vary from call to call.
9.1 The sep parameter is a string defining the set of characters used as separators.
9.2 The first parameter specifies a string containing zero or more tokens separated by one or more delimiters in the sep string.
9.3 The strtok function finds the next token in str, ends it with \0, and returns a pointer to this token. (Note:
The strtok function will change the string being manipulated, so the strings split using the strtok function are generally temporary copied content
And can be modified. )
9.4 If the first parameter of the strtok function is not NULL , the function will find the first mark in str , and the strtok function will save its position in the string.
9.5 If the first parameter of the strtok function is NULL , the function will start at the saved position in the same string and search for the next token.
9.6 If no more tokens exist in the string, a NULL pointer is returned .
#include <stdio.h>
#include <string.h>

int main()
{
	char arr[] = "[email protected]@666#777";
	char copy[30];
	strcpy(copy, arr);

	char sep[] = "@.#";
	char* ret = NULL;

	for (ret = strtok(copy, sep); ret != NULL; ret=strtok(NULL, sep))
	{
		printf("%s\n", ret);
	}

	return 0;
}

Error message report:

10. strerror (return error message)

char * strerror ( int errnum );
        
         errnum : error number.
        When the library function is executed, if a misplacement occurs, an error code will be stored in the variable errno, where errno is a global variable provided by the C language.
10.1 Return the error code and the corresponding error message. 
#include <stdio.h>
#include <string.h>

int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d: %s\n", i, strerror(i));//
	}
	return 0;
}

Character manipulation functions:

string functions
function
Returns true if its argument meets the following conditions
iscntrl
any control characters
isspace
Whitespace characters: space ' ' , form feed '\f' , line feed '\n' , carriage return '\r' , tab '\t' or vertical tab '\v'
even
Decimal number 0~9
self digit
Hexadecimal numbers, including all decimal numbers, lowercase letters a~f , uppercase letters A~F
islower
lowercase letters a~z
isupper
Capital letters A~Z
isalpha
Letter a~z or A~Z
the ice hall
Letter or number, a~z,A~Z,0~9
ispunct
Punctuation, any graphic character that is not a number or letter (printable)
isgraph
any graphic character
sprint
Any printable character, including graphic characters and whitespace

Character conversion:

1.towlower (lowercase -> uppercase)

int  tolower ( int c );

        

       c: The character to convert, convert to int or EOF.

#include <stdio.h>
#include <ctype.h>

int main()
{
    printf("%c\n", tolower('A'));
    printf("%c\n", tolower('s'));
    
    return 0;
}

2. toupper (uppercase -> lowercase)

int  toupper ( int c );

        

        c: The character to convert, convert to int or EOF.

#include <stdio.h>
#include <ctype.h>

int main()
{
	char arr[20] = { 0 };
	gets(arr);//遇到空格继续读

	char* p = arr;
	while (*p)
	{
		if (isupper(*p))// *p>='A' && *p<='Z'
		{
			*p = tolower(*p);//*p = *p+32;
		}
		p++;
	}
	printf("%s\n", arr);
	return 0;
}

Memory manipulation function:

1. memcpy (memory copy)

void * memcpy ( void * destination, const void * source, size_t num );
        
         destination: Pointer to the destination array in which to copy the content, typecast to a pointer of type void*.
        source: A pointer to the data source to be copied, converted to a pointer of type const void*.
        num : The number of bytes to copy.
                    size_t  is an unsigned integer type.
1.1 The function memcpy copies num bytes of data backwards from the location of the source to the memory location of the destination .
1.2 This function will not stop when encountering '\0' .
1.3 If the source and destination overlap in any way , the result of the copy is undefined.

#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	//将arr1中的内容,拷贝到arr2中
	memcpy(arr2, arr1, 40);
         int*  int*
	int i = 0;
	for (i = 0; i < 20; i++)
	{
		printf("%d ", arr2[i]);
	}

	return 0;
}

2. mommove (memory copy)

void * memmove ( void * destination, const void * source, size_t num );
        
         destination: Pointer to the destination array in which to copy the content, typecast to a pointer of type void*.
        source: A pointer to the data source to be copied, converted to a pointer of type const void*.
        num : The number of bytes to copy.

                  size_t  is an unsigned integer type.

The difference between 2.1 and memcpy is that the source memory block and target memory block processed by the memmove function can overlap.
2.2 If the source space and the target space overlap, you have to use the memmove function to deal with it.
#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	//             1 2 1 2 3 4 5 8 9 10
	memmove(arr1, arr1+2, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

3. memcmp (memory comparison)

int  memcmp ( const void * ptr1, const void * ptr2, size_t num );
        
         ptr1: Pointer to the memory block.
        ptr2: Pointer to the memory block.
         num : The number of bytes to compare.
3.1 Compare num bytes starting from ptr1 and ptr2 pointers;
3.2 The return value is as follows:

#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1,2,1,4,5,6 };
	int arr2[] = { 1,2,257 };
	int ret = memcmp(arr1, arr2, 10);
	printf("%d\n", ret);
	return 0;
}

4. memset (memory settings )

void *  memset ( void * ptr1,  int  value,  size_t  num );

        

        ptr1: Pointer to the memory block to be filled.

        value: The value to set. The value is passed as an int, but the function  fills the memory block with an unsigned char conversion of this value .

        num : The number of bytes to set to the value.
                    size_t  is an unsigned integer type.

#include <stdio.h>
#include <string.h>

int main()
{
	char arr[] = "hello bit";
	memset(arr+1,'x',4);//以字节为单位设置的
	printf("%s\n", arr);
	return 0;
}

The above is the analysis of personal learning insights and learning, welcome everyone to discuss in the comment area!

Thanks for the one-click three-connection guys! Thanks for the one-click three-connection guys! Thanks for the one-click three-connection guys!

                                              

Guess you like

Origin blog.csdn.net/weixin_71964780/article/details/132397713