- 实现一个自动管理内存的字符串库
- 用以下结构表达一个字符串:
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!");
}