Easily learn character functions and string functions

Find the length of a string
 strlen
string function with unlimited length
strcpy
strcat
strcmp
string function with restricted length introduction
strncpy
strncat
strncmp
 

1. Function introduction
1.1 strlen

size_t strlen ( const char * str );
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 ) .
2. The string pointed to by the parameter must end with '\0'.
3. Note that the return value of the function is size_t, which is unsigned (error-prone)
4. Learn the simulation implementation of the strlen function
 

#include <stdio.h>
int main()
{
const char*str1 = "abcdef";
const char*str2 = "bbb";
if(strlen(str2)-strlen(str1)>0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
return 0;
}

1.2 strcpy

char* strcpy(char * destination, const char * source );
1. The source string must end with '\0'.
2. The '\0' in the source string will be copied to the target space.
3. The target space must be large enough to ensure that the source string can be stored.
4. The target space must be variable.
5. Learn to simulate implementation.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr1[20] = "";
	char arr2[] = "hello bit";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

1.3 cracked

char * strcat ( char * destination, const char * source );
1. The source string must end with '\0'.
2. The target space must be large enough to accommodate the contents of the source string.
3. The target space must be modifiable.
 

#define _CRT_SECURE_NO_WARNINGS
//字符串追加的
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;

	//1. 找目标空间的\0
	while (*dest)
	{
		dest++;
	}
	//2. 追加
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr[20] = "hello ";
	char* p = "world";
	my_strcat(arr, p);

	printf("%s\n", arr);

	return 0;
}

1.4 strcmp
int strcmp ( const char * str1, const char * str2 );
the standard stipulates:
1. If the first string is greater than the second string, then return a number greater than 0
2. The first string is equal to the second String, then return 0
3. The first string is less than the second string, then return a number less than 0
 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;
}
int main()
{
	char arr1[] = "abq";
	char arr2[] = "abcdef";
	int ret = my_strcmp(arr1, arr2);

	if (ret>0)
		printf("arr1>arr2\n");

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

1.5 strncpy

char * strncpy ( char * destination, const char * source, size_t num );
1. Copy num characters from the source string to the target space.
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
 

1.6 strncat

char * strncat ( char * destination, const char * source, size_t num );
1. Append the first num characters of the source to the destination, plus the terminating null character

2. If the length of the C string in the source code is less than num, only the content before the termination null character is copie

#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
return 0;
}

1.7 strncmp


int strncmp ( const char * str1, const char * str2, size_t num );
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 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]);
}
return 0;
}

Guess you like

Origin blog.csdn.net/weixin_63246738/article/details/128636020