Character and string functions (2)

Key:

1. seeking string length
strlen
2. unrestricted length string functions
strcpy: copy
strcat: Append the string
strcmp: Comparison
3. limited length string functions described
strncpy: Copy
strncat: Append
strncmp: Comparison
4. Character string locating
strstr: find a string for a sub-string
strtok: cutting string
5. the error information of
the strerror
6. the operation of the character
7. the memory operation functions
memcpy: memory copy may not overlap
memmove: memory copy may overlap
memset: the specified character
memcmp: Compare

1.2 has been introduced in the past in summary articles:
[character and string functions (1) (https://mp.csdn.net/mdeditor/102987934#)

strncpy

Length limited
1.char ST * R & lt n- CPY (Where do you want char *, const char * Source, size_t num);
2. Note:
Copy num characters from the source string (source) to the target (Where do you want) space.
If the length of the source string is less than num, after completing the copy of the source string, 0 is added behind the target, until the first num.
strncpy does not help you ensure "\ 0", need to write their own
in addition to NULL, other formulations (Null NUll etc.) have expressed "\ 0"

int main()
{
	char arr[20] = { 0 };
	strncpy(arr, "hello bite", 3);
	printf("%s\n", arr);//hel
	return 0;
}

The difference between strcpy and strncpy

strncat- added

Length limited
1.char * strncat (char * destination, const char * source, size_t num);
the contents source is added to the destination, the additional characters in length num.

int main ()
{
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);//To be or not
  puts (str1);
  return 0;
}

strncmp

Length restricted
1.int a strncmp (const char * str1, str2 const char *, size_t num);
2. Comparative appear to different characters or other end of a string of characters or num compare all finished.

int main ()
{
  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  int n;
  puts ("Looking for R2 astromech droids...");
  for (n=0 ; n<3 ; n++)
  if (strncmp (str[n],"R2xx",2) == 0)
  {
    printf ("found %s\n",str[n]);
    //Looking for R2 astromech droids...
    //found R2D2
    //found R2A6
  }
  return 0;
}

strstr- find a substring in a string

* Strstr 1.char (const char *, const char *);
2.
strchr: find the child character in the string

int main()
{
	char arr[] = "abcdefabcdef";
	char* ret = strchr(arr, 'd');
	if (ret != NULL)
		printf("%s\n", ret); //defabcdef
	else
		printf("找不到\n");
	return 0;
}

strstr: find the position of the first occurrence of a substring in a string

int main()
{
	char arr[] = "abcdefabcdef";
	char* ret=strstr(arr, "def");//defabcdef
	printf("%s\n", ret);
	return 0;
}

Key:

char* my_strstr(const char* str1, const char* str2)
{
	const char* s1 = str1;
	const char* s2 = str2;
	const char* cur = str1;

	assert(str1);
	assert(str2);
	if (*str2 == '\0')
		return str1;

	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		//查找
		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return (char*)cur;
		cur++;
	}
	//找不到
	return NULL;
}

This code efficiency is low, if you want to improve efficiency. KMP algorithm can be implemented using algorithms. But KMP algorithm is complex, so in this not particularly explain.

strtok- cutting the string

* Strtok 1.char (STR char *, const char * On Sep);
2. Note:
(. 1) On Sep parameter is a string that defines the set of characters is used as a separator
(2) The first argument specifies a string, it contains zero or more strings from the one or more sep separator division mark.
(3) strtok function finds the next token in str and with \ 0 ends, the tag returns a pointer to a pointer.
( Strtok function will change the string is operated, so that the content can be modified and used to strtok generally slicing string temporary copy. )
The first argument (. 4) strtok function is not NULL , the function will find str that first marker , strtok function saves its position in the string.
(5) The first parameter strtok function is NULL , the function will start at the same position in the string to be stored, to find the next marker .
(6) If there are more mark exists string, returns a NULL pointer.

写法一:
int main()
{
	//char arr1[] = "192.168.0.1";
	char arr2[] = "[email protected]";//3个标记[zj baidu com]   2个分隔符[@ .]
	char buf[30] = { 0 };
	strcpy(buf, arr2);//拷贝数据,处理buf数组的内容
	printf("%s\n", strtok(buf, "@."));//zj 从第一个标记开始到分隔符结束
	printf("%s\n", strtok(NULL, "@."));//baidu 从第二个标记开始到分隔符结束
	printf("%s\n", strtok(NULL, "@.")); //com
	return 0;
}

写法二:
int main()
{
	char arr2[] = "[email protected]";//3个标记[zj baidu com]   2个分隔符[@ .]
	char buf[30] = { 0 };
	char* ret = NULL;
	strcpy(buf, arr2);
	for (ret = strtok(buf, "@."); ret != NULL; strtok(NULL, "@."))
	{
		printf("%s\n", ret);
		//zj 
		//baidu
		//com
	}
	return 0;
}

写法三:
int main()
{
	char arr2[] = "[email protected]";//3个标记[zj baidu com]   2个分隔符[@ .]
	char buf[30] = { 0 };
	char* ret = NULL;
	const char* sep = "@.";
	strcpy(buf, arr2);
	for (ret = strtok(buf, sep); ret != NULL; strtok(NULL, sep))
	{
		printf("%s\n", ret);
		//zj 
		//baidu
		//com
	}
	return 0;
}

strerror

* The strerror 1.char (int errnum);
2. header #include <errno.h> // must contain
3.

int main()
{
	char* ret = strerror(errno);
	printf("%s\n", ret);
	return 0;
}

Error code summary
4.

int main ()
{
  File *p= fopen ("unexist.ent","r");
  if (p File == NULL)
    printf ("Error opening file unexist.ent: %s\n",strerror(errno));
    //errno: Last error number
  return 0;
}
fclose();
p=NULL;

Character Classification Functions

1. The header file: #include <ctype.h>
2.

function If his argument with the following conditions are true returns
iscntrl Any control character
isspace Blank characters: space '' feed '\ F', newline '\ n-' Enter '\ R & lt' tab '\ t' or vertical tab '\ V'
isdigit Decimal numbers 0 to 9
isxdigit Hexadecimal digits, including all decimal numbers, lowercase letters af, capital letters AF
islower Lowercase letters az
isupper Uppercase letters AZ
Islf Letters az or AZ
isalnum Letters or numbers, az, AZ, 0-9
ispunct Punctuation, do not belong to any number or letter of the graphic character (printable)
isgraph Any graphic characters
printout Any printable characters, including blank characters and graphic characters
int main()
{
	int ret = isdigit('0');
	printf{ "ret=%d\n",ret };
	return 0;
}

Character conversion functions

1.头文件:#include <ctype.h>
2.
int tolower ( int c );
int toupper ( int c );
3.

int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    if (isupper(c)) 
        c=tolower(c);
    putchar (c);
    i++;
  }
  return 0;
}

memcpy- memory copy

Memcpy * 1.void (void * destination, source const void *, size_t num);
2. Note:
functions memcpy copy source from a position rearwardly of num bytes of data to the destination memory location.
This function in the face of '\ 0' time and will not stop.
If the source and destination have any overlap, the results are replicated undefined. (VS may overlap the copy)
3.

void* my_memcpy(void* dest, const void* src, size_t count)
{
	void* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	//拷贝
	while (count)
	{
		//拷贝一个字节
		*(char*)dest = *(char*)src;
		((char*)dest)++;
		((char*)src)++;
		//++(char*)dest;
		//++(char*)src;
		count--;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[10] = { 0 };
	my_memcmp(arr2, arr1, 20);// 1 2 3 4 5   20为字节数(4*5)
	return 0;
}

If you need to copy 1234 to position 3456:
my_memcmp (of arr1 + 2, of arr1, 16); The code output is: 1212127890 in this case, an error of.
Cause:
The copy time 1 to 3, and 3 will change, at this time of 3 to 5 and then copy when the copy is 1, not 3.
When the copy 2 to 4, 4 will change, in this case 4 then copied to 6 when the copy is two, not four.
After 12 copies, the array becomes: 1212567890

In this case it is necessary to move forward from the copy will now be 4 to 6 copies, copy and then sequentially forward until 1 to 3 copies.
But if you need 3,456 to 1,234 copies, the copy will be a problem backwards. Memmove so at this time we need to use the function.

memmove

* Memmove 1.void (Where do you want void *, const void * Source, size_t NUM);
2. Note:
memmove function processing of the source memory blocks and the target memory block is to be overlapped to. If the source space and a target space overlap, have to use memmove function processing.

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	//把2 3 4 5拷贝到1 2 3 4
	mommove(arr1 + 2, arr1, 16); //3 4 5 6 5 6 7 8 9 0
	return 0;
}
void* my_mommove(void* dest, const void* src, size_t count)
{
	void* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	if (dest < src)
	{
		while (count)
		{
			//拷贝一个字节
			*(char*)dest = *(char*)src;
			dest=((char*)dest)+1;
			src=((char*)src)+1;
			count--;
		}
	}
	else
	{
		//后->前
		while (count--)
		{
			*((char*)dest + count) = *((char*)src + count);
		}
	}
	return ret;
}

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	my_mommove(arr1 + 2, arr1, 16); //3 4 5 6 5 6 7 8 9 0
	my_mommove(arr1, arr1 + 2, 16); //1 2 1 2 3 4 7 8 9 0
	return 0;
}

Here Insert Picture Description

memcmp

Memcmp 1.int (ptr1 const void *, const void * ptr2, size_t COUNT);
2. Note:
Comparative ptr1 and from ptr2 pointer bytes starting num
same returns num bytes 0
ptr1> ptr2: returns> 0
ptr1 <ptr2: Back <0

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[10] = { 0 };
	memcmp(arr2, arr1, 20);//20为字节数(2*10)
	return 0;
}

The memset- each byte is initialized to a specified character space

* Memset 1.void (void * dest, int the src, size_t COUNT);
COUNT: bytes src: specified character
2. Note:
3.

将每一个字节都设置成0
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
	memset(arr, 0, 40);
	return 0;
}
Da
Published 37 original articles · won praise 3 · Views 1095

Guess you like

Origin blog.csdn.net/weixin_43264873/article/details/102999186