C language character functions and string functions

The C language itself does not have a string type, but in the C language, operations on strings are very frequent. In C, strings are usually in character arrays or constant strings. under. Let's introduce some commonly used functions for strings.

One, character class

For characters, it is often encountered to determine whether a character is lowercase, determine whether a character is uppercase, determine whether a character is a number, and convert lowercase letters to uppercase, and uppercase letters to lowercase.

1, isdigit function

The isdigit function is used to determine whether it is decimal 0~9, and the header file is ctype.h. If yes, return 0.

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

int main(void)
{
	int a = 10;
	if (isdigit(a) == 0)
	{
		printf("是数字!");//将会打印结果。
	} 
	return 0;
}

2, islower function && isupper function && isalpha function && isalnum function

The islower function is used to determine whether it is a lowercase letter a~z. The header file is ctype.h. If so, return a non-zero number.

The isupper function is used to determine whether it is a capital letter a~z. The header file is ctype.h. If so, return a non-zero number.

The isalpha function is used to judge whether it is the letters a~z, A~Z. The header file is ctype.h. If so, return a non-zero number.

The isalnum function is used to determine whether it is a letter or a number. The header file is ctype.h. If so, return a non-zero number.

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

int main(void)
{
	char a = 'e';
	if (islower(a))
	{
		printf("是小写字母!");//将会打印出结果
	}
	return 0;
}
#include<stdio.h>
#include<ctype.h>

int main(void)
{
	char a = 'E';
	if (isupper(a))
	{
		printf("是大写字母!");//将会打印出结果
	}
	return 0;
}
#include<stdio.h>
#include<ctype.h>

int main(void)
{
	char a = 'E';
	if (isalpha(a))
	{
		printf("是字母!");//将会打印出结果
	}
	return 0;
}
#include<stdio.h>
#include<ctype.h>

int main(void)
{
	char a = 'e';
	if (isalnum(a))
	{
		printf("是字母或数字!\n");//将会打印出结果
	}
	return 0;
}

3, tolower function && toupper function

The tolower function converts uppercase letters to lowercase letters. The header file is ctype.h.

The toupper function converts lowercase letters to uppercase letters. The header file is ctype.h.

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

int main(void)
{
	char a = 'E';
	printf("%c\n", tolower(a));//e
	return 0;
}
#include<stdio.h>
#include<ctype.h>

int main(void)
{
	char a = 'e';
	printf("%c\n", toupper(a));//E
	return 0;
}

Second, the string class

String class function header files are all string.h.

1, strlen function

The strlen function is used to find the length of a string.

size_t strlen ( const char * str );

The string ends with '\0', and the strlen function returns the number of characters (excluding '\0') that appear before '\0' in the string. The string pointed to by the parameter must end with '\0'. Note that the return value of the function is size_t, which is unsigned (error-prone).

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

int main(void)
{
	char* a = "abcdef";
	printf("%d\n", strlen(a));//6
	return 0;

2, strcpy function && strncpy function

The strcpy function is used to copy strings.

The difference between strncpy function and it is that this function can copy according to the specified length.

char* strcpy(char * destination(destination string), const char * source(source string));

char * strncpy ( char * destination, const char * source, size_t num );

The source string must end with '\0'. This function will copy the '\0' in the source string to the destination space. The destination space must be large enough to hold the source string. The target space must be mutable. 

int main(void)
{
	char a[] = "abcdef";
	char b[20] = { 0 };
	strcpy(b, a);
	printf("%s\n", b);//abcdef
	return 0;
}
int main(void)
{
	char a[] = "abcdef";
	char b[20] = { 0 };
	strncpy(b, a, 2);
	printf("%s\n", b);//ab
	return 0;
}

3, strcat function && strncat function

The strcat function is used to concatenate strings.

The strncat function can follow the specified connection.

char * strcat ( char * destination, const char * source );

char * strncat ( char * destination, const char * source, size_t num ); 

The source string must end with '\0'. This function will copy the '\0' in the source string to the destination space. The destination space must be large enough to hold the source string. The target space must be mutable. 

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

int main(void)
{
	char a[] = "abcdef";
	char b[20] = { 'a', 'a', 'a'};
	strcat(b, a);
	printf("%s\n", b);//aaaabcdef
	return 0;
}
int main(void)
{
	char a[] = "abcdef";
	char b[20] = { 'a', 'a', 'a' };
	strncat(b, a, 2);
	printf("%s\n", b);//aaaab
	return 0;
}

4, strcmp function && strncmp function

The strcmp function is used to compare strings.

The strncmp function is to compare until another character is different or the end of a string or all num characters are compared.

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

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

standard regulation:

        If the first string is greater than the second string, return a number greater than 0

        If the first string is equal to the second string, return 0 if the first string is less than

        the second string, returns a number less than 0

This comparison does not compare the length of the string, but the ASCII value of each letter of the string.

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

int main(void)
{
	char a[] = "abcdef";
	char b[20] = "bcdef";
	int ret = strcmp(a, b);
	printf("%d\n", ret);//-1
	return 0;
}

If the length is compared, the returned value should be a number greater than 0, and the result is a number less than 0, which means that this function is not comparing the length of the string, but the ASCII code value. The ASCII value of a is 97 and b is 98, so the result is -1;

int main(void)
{
	char a[] = "abcdef";
	char b[] = "abcdef";
	int ret = strncmp(a, b, 2);
	printf("%d\n", ret);//0
	return 0;
}

5, strstr function

The strstr function is used to determine whether a string exists in the main string (matching of strings). If the match is successful, a pointer to the position of the first occurrence in the string. Returns the main string if it is an empty substring, otherwise returns NULL.

char * strstr ( const char *str2, const char * str1);

int main(void)
{
	char a[] = "abcdef";
	char b[20] = "def";
	char* ret = strstr(a, b);
	printf("%s\n", ret);//def
	return 0;
}

6, strtok function

The strtok function divides the string according to certain delimiters.

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

        The sep parameter is a string that defines the set of characters to use as separators. The first parameter specifies a string containing zero or more tokens separated by one or more delimiters in the sep string. 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 string segmented by the strtok function is generally the content of a temporary copy and can be modified.)

        If the first argument to the strtok function is not NULL, the function will find the first token in str and the strtok function will save its position in the string. The first parameter of the strtok function is NULL, and the function will start at the saved position in the same string and look for the next token. If there are no more tokens in the string, a NULL pointer is returned. To put it simply, for example: 123@456#, after this function splits 123 for the first time, the address after 123 of the string is reserved, and at the same time, 123 is set to empty. The next time is to start from NULL, after dividing 456, perform similar operations, and return NULL until the division is complete.

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

int main(void)
{
	char* a = "hello@world#.my&dream";
	char* str = NULL;
	char b[40] = { 0 };
	strcpy(b, a);/临时拷贝
	for (str = strtok(b, "@#."); str != NULL; str = strtok(NULL, "@#.&"))
	{
		printf("%s\n", str);//-1
	}
	
	return 0;
}

 7, strerror function (understand)

Returns the error code and the corresponding error message. The header file is errno.h.

char * strerror ( int errnum );

 3. Other types

The above functions are only valid for strings. If int and other types want to perform operations such as copying and concatenation, is there any function for them? The answer is yes. These functions are called memory functions. As long as it is in memory, it can be operated. Below we introduce three memory operation functions, which are used for copying, overlapping copying and comparison. It should be noted that in the parameters of these functions, the unit of num is bytes.

1, memcpy function

void * memcpy ( void * destination, const void * source, size_t num );

The function memcpy starts from the location of source and copies num bytes of data backward to the memory location of destination. This function does not stop when it encounters '\0'.

int main(void)
{
	int a[10] = { 0 };
	int b[10] = { 1,2,3,4,5,6,7,8,9,0 };

	memcpy(a, b, 10 * sizeof(b[0]));
	for (int i = 0; i < 10; ++i)
	{
		printf("%d ", a[i]);//1 2 3 4 5 6 7 8 9 0
	}
	return 0;
}

2, the memmove function

void * memmove ( void * destination, const void * source, size_t num );

The difference with memcpy is that the source memory block and the target memory block processed by the memmove function can overlap. If the source space and the target space overlap, you have to use the memmove function to deal with it.

int main()
{
	char str[] = "memmove can be very useful......";
	memmove(str + 20, str + 15, 11);
	puts(str);//memmove can be very very useful.
	return 0;
}

3, memcmp function

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

The return value is the same as the strcmp function.

int main()
{
	int a[10] = { 1,2,3,4,5 };
	int b[10] = { 1,2,5,7,9 };
	int ret = memcmp(a, b, 2 * sizeof(a[0]));
	printf("%d\n", ret);//0
	return 0;
}

   

Guess you like

Origin blog.csdn.net/Naion/article/details/122624356