C language function simulation implementation

content

1. Simulate the implementation of strncat

1.1 Code writing 

1.2 The results are as follows

2. Simulate strncpy

2.1 Code writing

2.2 The results are as follows

3. Find a single dog

3.1 Code writing

3.2 The results are as follows


 

1. Simulate the implementation of strncat


Among the system library functions, there is the strncat function, which is used for appending strings, that is, appending a string after a string. Its function prototype is:

char *strncat(char *strDest,const *strSource,size_t count);

*strDest is the target string *strSource is the source string, count is the number of strings to be appended.

1.1 Code writing 

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef unsigned int uint;
char *my_strncat(char *dest, const char *src, uint count)//模拟实现strncat函数
{
 assert(dest);
 assert(src);
 int *ret = dest;
 while (*dest)
 {
  dest++; //找到dest中的\0
 }
 while (count--)
 {
  *dest++ = *src++;
 }
 *dest = '\0';
 return ret;
}
//程序测试
int main()
{
 char arr[20] = "abcdef";
 int len = strlen(arr);
 my_strncat(arr, arr, len);
 printf("%s\n", arr);
 system("pause");
 return 0;
}

1.2 The results are as follows

 


 

2. Simulate strncpy

char *strncpy(char *strDest,const *strSource,size_t count);

*strDest is the target string *strSource is the source string, count is the number of characters that need to be copied from the source string to the target string. 

Note: If the length of the source string is less than count, add 0 to the target string after copying the source string, until the count is

2.1 Code writing

char * My_strncpy(char * dest, const char *src, size_t n)
{
    assert(dest);
    assert(src);

    char *ret = dest;
    while (n--)
    {
        *dest++ = *src++;
    }
    return ret;
}
int main()
{
    char str1[20] = "123456789";
    char str2[20] = "abcde";
    printf("%s", My_strncpy(str1, str2, 3));

}

2.2 The results are as follows

3. Find a single dog

Only two numbers in an array appear once, all other numbers appear twice.

Write a function to find these two numbers that occur only once.

3.1 Code writing

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<assert.h>
void find_two_diff_num(int arr[], int sz, int *p1, int *p2)
{
	int i = 0;//循环变量
	int ret = 0;
	int pos = 0;
	*p1 = 0;//数字1的地址
	*p2 = 0;//数字2 的地址
	//1.把所有数字异或 
	for (i = 0; i < sz; i++)
	{
		ret ^= arr[i];//循环到最后一次的结果是5^6,即就是101^110=011
	}
	//2.找ret二进制中为1的一位
	for (i = 0; i < 32; i++)
	{
		if (((ret >> i) & 1) == 1)//上一步两个数异或的后两位为1
		{
			pos = i;
			break;
		}
	}
	//分组
	for (i = 0; i < sz; i++)
	{
		if (((arr[i] >> pos) & 1) == 1)
		{
			(*p1) ^= arr[i];
		}
	}
	(*p2) = (*p1) ^ ret;
}


int main()
{
	int arr[] = { 1, 2, 5, 6, 8, 1, 6, 2, 9, 8 };
	int sz = sizeof(arr) / sizeof(arr[0]);//数组大小
	int num1 = 0;//数字1
	int num2 = 0;//数字2
	find_two_diff_num(arr, sz, &num1, &num2);
	printf("num1=%d,num2=%d\n", num1, num2);
	system("pause");
	return 0;
}

3.2 The results are as follows

 

Guess you like

Origin blog.csdn.net/weixin_53939785/article/details/124062080