Character and string functions (1)

Key:

1. seeking string length
strlen
2. unrestricted length string functions
strcpy
strcat: Append the string
strcmp
3. limited length string presentation function
strncpy
strncat
a strncmp
4. string search
strstr: find in a string a substring
strtok: cutting string
5. The error information of
the strerror
6. The operation of the character
7. The memory operation functions
the memcpy
memmove
Memset
memcmp

strlen

1. size_t strlen (const char * STR);
2. Note:
the string has' \ 0 'as an end flag, strlen function returns the string' \ 0 'the number of characters appearing in front of (does not contain' \ 0 ').
Parameter points to the string must be '\ 0' end.
Note that the function returns a value of size_t, unsigned (error-prone), will not be negative.

#include<stdio.h>
int main()
{
	char * p1 = "abcdef";
	char* p2 = "abc";
	if (strlen(p2) - strlen(p1) > 0)//3-6=-3
	{
		printf("p2>p1");
	}
	else
	{
		printf("p2<=p1");
	}
	return 0;
}

Output: P2> P1
strlen (P2) is unsigned, strlen (p1) is also unsigned. Unsigned - unsigned = unsigned. 3-6 = -3 At this time it is represented by a very large positive number.
For negative output: cast: if ((int) strlen ( p2) - (int) strlen (p1)> 0)

Three Kind of Writing 3.my_strlen of:
own realization:
(1)
size_t my_strlen (const char * str)
{
size_t COUNT = 0;
// null pointer asserts
the Assert (str = NULL!);
The while (* str)
{
COUNT ++;
str ++ ;
}
return COUNT;
}

(2)
size_t my_strlen(const char* str)
{
const char* start = str;
while (*str)
{
str++;
}
return str - start;
}

(3)
size_t my_strlen(const char* str)
{
assert(str != NULL);
if (*str != ‘\0’)
return 1 + my_strlen(str + 1);
else
return 0;
}

strcpy

Unrestricted length
1.char strcpy * (char * Where do you want, const char * Source);
2. Note:
the source string must end with a '\ 0' .
Copies the source string '\ 0' copied to the target space.
Target space must be large enough to ensure that the source string can be stored.
Target space must be variable.

自己实现:
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr[20] = { 0 };
	char arr[] = { "L","H","Z","\0" };
	//strcpy(arr, "hello word");
	//printf("%s\n", arr);
	printf("%s\n", strcpy(arr,arr2)); //链式访问
	return 0; 
}

Here Insert Picture Description

strcat

Unrestricted length
1.char * strcat (Where do you want char *, const char * Source);
2. Note:
the source string must end with a '\ 0'.
The target area must be large enough to accommodate the contents of the source string.
Target space must be modified.

自己实现:
char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	//1.找目标空间中的'\0'
	/*两种写法:
	while(* dest++ != '\0')
	 {
     }
	  dest--;
	 */
	while (*dest != '\0')
	{
		dest++;
	}
	//2.追加内容
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr[20] = "hello";
	strcat(arr, "word");
	printf("%s\n", arr);
	return 0;
}

strcmp

Unrestricted length
1.int strcmp (str1 const char *, const char * str2);
2. standard:
first string is greater than the second string, a number greater than 0 is returned
first string equal to the two string, returns 0 if
the first string is less than the second string, number less than 0 is returned

int my_strcmp(const char*s1,const char*s2)
{
	assert(s1);
	assert(s2);
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	//l两种写法:
	(1)return *s1 - *s2;
	//(2)if (*s1 > * s2)
		return 1;
	else
		return -1;
}

int main()
{
	char* p1 = "abcdef";
	char* p2 = "abq";
    int ret =my_strcmp(p1, p2);
	printf("ret=%d\n", ret);

	return 0;
}
Da
Published 37 original articles · won praise 3 · Views 1100

Guess you like

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