自动管理内存的字符串库实现(C语言)

  • 实现一个自动管理内存的字符串库
  • 用以下结构表达一个字符串:
typedef struct {
	char* content;
	int len;
}string;
  • 这里的len表达字符串中的字符数量,同时也就是content所指的内存大小
  • 这里的content指向一块动态分配的内存,其中的每一个字节都是一个字符,并没有C字符串的表示结尾的'\0'
  • 具有基本的字符串处理函数

//string.h

//The header file 
//string.h
#include <stdio.h>
#include <stdlib.h>

//String structure
typedef struct {
	char* content;
	int len;
}string;

/**
Initialize string
@param s is the string to be initialized
@param source is the C string used to initialize the content*/
void str_init(string* dest);
void str_create(string*dest, const char* source);

/**
Release string
@param s The string that is releasing*/
void str_free(string *dest);

/**
String length
@param s calculates the length of the string
@return The length of the string*/
unsigned int str_len(const char*str);
unsigned int str_len2(const string*dest);

/**
String copy
@param dest destination string
@param source source source source string
@return destination string*/
char* str_copy(string*dest, const char*source);

/**
String connection
@param dest destination string
@param source source source source string
@return destination string*/
void str_concat(string *dest, char *source);

/**
Read a line from standard input
@param s The string that is writing to*/
void str_readline(string *dest);

/**
Output a string on standard output
@param s The string that is going to output*/
void str_print(string* dest);

//string.c

#include "string.h"

//Initialize string
void str_init(string* dest)
{
	dest->content = NULL;
	dest->len = 0;
}

void str_create(string*dest, const char* source)
{
	if (source == NULL)
		return;
	else {
		int count = str_len(source);
		dest->content = (char*)calloc(count + sizeof(char), sizeof(char));
		dest->len = count + sizeof(char);
		dest->content = str_copy(dest, source);
	}
}

//Release string
void str_free(string *dest)
{
	free(dest->content);
	free(dest); 
}

//Gets the length of the string
unsigned int str_len(const char*str)
{
	if (str == NULL)
		return 0;
	int count = 0;
	while (*str++ != '\0')
		count++;
	return count;
}

unsigned int str_len2(const string*dest)
{
	char *str = dest->content;
	if (str == NULL) return 0;
	int count = 0;
	while (*str++ != '\0') count++;
	return count;
}

//String copy
char* str_copy(string*dest, const char*source)
{
	if (dest->content == NULL || source == NULL)
		return NULL;
	char* strTmp = dest->content;
	int sourLen = str_len(source);
	while (*source != '\0')
		*strTmp++ = *source++;
	*(dest->content + sourLen) = '\0';
	dest->len = sourLen;
	return dest->content;
}

//String connection
void str_concat(string *dest, char *source)
{
	char *des = dest->content;
	while (*des != 0)
		des++;
	while (*des++ = *source++);
}

//Read string
void str_readline(string *dest)
{
	char p[100];
	scanf("%s", p);
	str_copy(dest, p);
}

//Print string
void str_print(string* dest)
{
	if ((dest) != NULL)
		printf("%s\n", dest->content);
	else
		printf("NULL\n");
}

//main.c

//Test in the main function
// main.c
#include <stdlib.h>
#include "string.h"

int main()
{
	int length = 100;
	string *strs;
	strs = (string*)malloc(1000);
	char str[100];
	char oop[100];
	char source[100];

	//Initialize string
	printf(">> Initialize string:\n");
	str_init(strs);//Initialize the string to null
	printf(">> The current string is:");
	str_print(strs);//Print string
	printf(">> The current string length is:%d\n", str_len2(strs));
	printf("\n>> Please enter the initialization content:");
	scanf("%s", source);
	str_create(strs, source);//Initializes the string content
	printf(">> The current string is:");
	str_print(strs);//Print string 
	printf(">> The current string length is:%d\n", str_len2(strs));//Print the length of the string 

	//String copy
	printf("\n>> Please enter the copied string:");
	scanf("%s", oop);
	str_copy(strs, oop);
	printf(">> The current string is:");
	str_print(strs);//Print string 
	printf(">> The current string length is:%d\n", str_len2(strs));

	//String connection
	printf("\n>> String connection:\n");
	printf(">> Please enter the connection string:");
	scanf("%s", str);
	str_concat(strs, str);
	printf(">> The current string is:");
	str_print(strs);
	printf(">> The current string length is:%d\n", str_len2(strs));

	//Read string
	printf("\n>> Read string:\n");
	printf(">> Please enter the read string:");
	str_readline(strs);
	printf(">> The current string is:");
	str_print(strs);
	printf(">> The current string length is:%d\n\n", str_len2(strs));

	//Release string
	str_free(strs);
	printf(">> The current string is:");
	str_print(strs);
	printf(">> The current string length is:%d\n", str_len2(strs));
	printf(">> String released successfully!");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值