String function and character function

Character classification function

Function returns true if its arguments meet the following conditions
iscntrl any control character
isspace blank characters: space ',' page feed '\ f', line feed '\ n', carriage return '\ r', tab '\ t' or vertical tab '\ v'
isdigit decimal digits 0 ~ 9
isxdigit hexadecimal digit, including all decimal digits, lowercase AF and uppercase AF
islower small letter a~z
isupper capital letters A~Z
isalpha letter AZ or AZ
isalnum letter or number, az,AZ,0~9
ispunct punctuation mark, any graphic character not belonging to numbers or letters (printable)
isgraph any graphic character
isprint any printable character, including graphic characters and white space characters

The Works of Liezi

1.isdigit

#include<stdio.h>
#include<ctype.h>
int main()
{
	char ch = '#';
	int ret = isdigit(ch);
	printf("%d\n", ret);
	return 0;
}

#include<stdio.h>
#include<ctype.h>
int main()
{
	char ch = '0';
	int ret = isdigit(ch);
	printf("%d\n", ret);
	return 0;
}


Note; isdigit returns a value other than 0 if it is a numeric character, and 0 if it is not a numeric character

2.islower


Note: the usage is similar to isdigit above

Character conversion function

1.int tolower ( int c ); Turn lowercase
2.int toupper ( int c ); Capitalize

example

#include<stdio.h>
#include<ctype.h>
int main()
{
	char arr[20] = { 0 };
	scanf("%s", arr);
	int i = 0;
	while (arr[i] != '\0')
	{
		if (isupper(arr[i]))
		{
			arr[i] = tolower(arr[i]);
			
		}
		printf("%c", arr[i]);
		i++;
	}
	return 0;
}

Note: the usage of tower is similar to that of tower

Memory function (key)

memcpy

This function needs to call string H header file

usage

void * memcpy ( void * destination, const void * source, size_t num );
1. The function memcpy copies num bytes of data backward from the source to the memory location of destination. (that is, destination is copied, source is copied, and num is the number of bytes)
2. This function will not stop when it encounters' \ 0 '.
3. If there is any overlap between source and destination, the copied result is undefined.

#include<string.h>
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	memcpy(arr2, arr1, 20);
	return 0;
}

Simulation Implementation of memcpy

#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dst, const void* src, size_t num)
{
	void* ret = dst;
	assert(dst && src);
	
	while (num--)
	{
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return ret;
}

Cited examples

#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dst, const void* src, size_t num)
{
	void* ret = dst;
	assert(dst && src);
	
	while (num--)//4 3 2 1
	{
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
		//*(char*)dst++ = *(char*)src++;
	}
	return ret;
}

int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	my_memcpy(arr1+2, arr1, 20);
	return 0;
}


analysis

Note: the memcpy function should copy non overlapping memory

memmove

usage

void * memmove ( void * destination, const void * source, size_t num );
1. The difference from memcpy is that the source memory block and target memory block processed by memmove function can overlap.
2. If the source space and the target space overlap, you have to use the memmove function.

#include<stdio.h>
#include<string.h>
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	memmove(arr1+2, arr1, 20);
	return 0;
}


Note: memcpy - as long as the non overlapping copy is implemented, and the VS implementation can copy both non overlapping and overlapping memory

Implementation of simulated memmove function

analysis

#include<stdio.h>
#include<assert.h>
void* my_memmove(void* dest, const void* source, size_t num)
{

	assert(dest && source);
	void* ret = dest;
	void* r = dest;
	void* s = source;
	if (dest < source)
	{
		while (num--)
		{
			*(char*)dest = *(char*)source;
			dest = (char*)dest + 1;
			source = (char*)source + 1;
		}
		return ret;
	}
	else
	{
		while (num--)
		{
			r=(char*)dest + num;
			s=(char*)source + num;
			*(char*)r = *(char*)s;
			//*((char*)dest + num) = *((char*)source + num);
		}
		return ret;
	}
}

memmcmp (memory comparison)

usage

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
1. Compare num bytes starting from ptr1 and ptr2 pointers
2. The return value is similar to strcmp

int main()
{
	float arr1[] = { 1.0,2.0,3.0,4.0 };
	float arr2[] = { 1.0,3.0 };
	int ret=memcmp(arr1, arr2, 8);
	printf("%d\n", ret);
	return 0;
}


memset (memory setting)

usage

void * memset ( void * ptr, int value, size_t num );
It refers to that the content of num bytes pointed to by the first ptr is set to the value of the specified value

int main()
{
	int arr[10] = { 0 };
	memset (arr,1,20 );
	return 0;
}

Keywords: C C++ Programming string

Added by skyagh on Sun, 30 Jan 2022 01:26:32 +0200