[C learning notes] C language realizes Pos(), Copy(), Delete() functions of Delphi

Posted by Jeb. on Fri, 20 Dec 2019 18:00:49 +0100

This code was originally created by blogger Huang Renlai. For reprint, please indicate the source: https://blog.csdn.net/weixin_43334745


Recently, I am learning C, deeply aware of the power (and danger) of the C pointer. If I master it well, I will control the memory like a fish in water; if I master it badly, I will get random code, if the program exits abnormally, and if I don't master it well, I will crash the blue screen of the system. It is said that the pointer is the soul of C, which is not too much.

Today, take the pointer for a test and write three string processing functions, namely, Pos(), Copy(), Delete() in Delphi. The reason is obvious. Brother is also a big fan of Delphi.


#include <stdio.h>
#include <string.h>

/*Return the location of substr of str substring*/
int Pos(const char* str, const char* substr)
	char* res = strstr(str, substr);
	if (res) return res - str;	else return 0;

/*Returns a string of count characters from iStartPos in str*/
char *Copy(const char* str, int iStartPos, int count)
	if (iStartPos < 0) iStartPos = 0;
	if (count <= 0) return "";
	if (iStartPos >= strlen(str)) return "";

	int ileft = strlen(str) - iStartPos;
	int isize = ileft >= count ? count : ileft;
	if (isize<=0) return ""; 

	char* res = (char*)calloc(isize + 1, sizeof(char)); /*Allocate memory and fill it with \ 0;*/
	if (!res) return "";

	int i = 0;
	while (i<isize)
		*(res + i) = *(str + iStartPos + i); /*Copy isize characters one by one from the specified position in str;*/
	//*(RES + isize) = '\ 0'; / * this sentence can be used for calloc allocation; this sentence is required for malloc allocation*/
	return res;

/*Delete str from the iStartPos position, a total of count characters, and return*/
char* DeleteStr(const char* str, int iStartPos, int count)
	int len = strlen(str);
	if (iStartPos <= 0) iStartPos = 0;
	if ((count <= 0) || (iStartPos >= len))  return str;

	if (count >= len - iStartPos) count = len - iStartPos;
	int isize = len - count ;/* The length of the final result;*/
	char* res = (char*)calloc(isize + 1, sizeof(char));  /*Allocate the memory of the original string length + 1 and fill it with \ 0;*/
	int i = 0; int j = 0;
	while (i<len)
		if ((i >= iStartPos) && (i < iStartPos + count)) /*If it is within the scope of deletion, skip directly*/
		*(res + j) = *(str + i); /*Copy the corresponding characters;*/
		i++; j++;
	//*(res + j) = '(RES + J) = '\ 0''; / * it is not necessary to allocate memory with calloc; it is necessary to allocate memory with malloc*/
	return res;

int main()
	/*            012345678901234567   ->Location reference*/
	char *str1 = "C language is fun!";
	char *str2 = "fun";
	/*Returns the first occurrence of str2 in str1;*/
	printf("test Pos(str1,str2)=%d\n",Pos(str1, str2)); 

	/*Return 4 characters after the start of the second bit in str1;*/
	printf("test Copy(str1,2,8)=%s\n",str1,Copy(str1,2,4)); 

	/*Delete 9 strings in str1 starting from and after the second character*/
	printf("test Delete(%s,2,9)=%s\n",str1,DeleteStr(str1,2,9)); 
	return 0;


A simple test:

Keep a problem: after malloc or calloc is used to allocate memory in a function, it usually needs to be free, but after the function return, it cannot be free. If it is free before the return, it will return an error result, So?

Topics: Delphi C