C language - string sequential storage representation and basic operation implementation

Posted by eatadi on Tue, 07 Sep 2021 05:01:29 +0200

Three storage representations of 1 string

     String, i.e. string. It should be noted that there is no string data type in C language, but it is implemented as a data structure - "content limited linear table", and the concepts of empty string, blank string, string length, substring and main string are agreed [see data structure (C language version), edited by Yan Weimin and WU Weimin].
     There are three main storage representations of strings,
    [ 1] Sequential storage means: a character array (a continuous memory storage space) is used to store the contents of the string. If the maximum length has been defined, it cannot be modified( This is also a better representation of string content originally agreed in C language)
    [ 2] Heap allocation storage means: malloc and free functions are used to operate the heap area to dynamically allocate and release the heap memory, and this storage space is used to store the contents of the string;
    [ 3] Block chain storage: that is, multiple string "fragments" located in different storage spaces are connected through pointers in the form of linked list, and "pieced together" into a complete string,
     Only the first one is introduced here, that is, the sequential storage representation of strings.

Sequential storage representation and basic operation of 2 strings

     This part will paste the code directly,
    [ Because this is an operation on array elements, I'm lazy. I directly use some library functions provided in the C language string.h header file to simply implement some basic operations. The part that needs to be written in detail will be reflected when the heap allocation of the string is implemented]

2.1 header file declaration

/**
 * Implementation of sequential storage of strings
 */
#include <string.h>
#define  MAXLEN 255
//Structure definition - represents a string structure
typedef struct{
	char ch[MAXLEN+1];//The character array [+ 1] storing string contents is used to store the end flag '\ 0']
	int length;//The length of the current string
}SString;

/**
 * Copy string T to chars
 */
int StrAssignS(SString* T,char* chars);

/**
 * Get string length
 */
int StrLengthS(SString T);

/**
 * Empty judgment of string
 */
int StrIsEmptyS(SString T);

/**
 * Serial connection operation
 */
int StrConcatS(SString* T,char* s1,char* s2);

/**
 * Intercept substring
 */
int SubStringS(SString* T,SString src,int pos,int len);

/**
 * String emptying
 */
int ClearStringS(SString* T);

/**
 * String comparison
 */
int StrCompareS(SString T1,SString T2);

2.2 function implementation

#include "SString.h"

/**
 * Copy string T to chars
 */
int StrAssignS(SString* T,char* chars){
	//Empty original content
	memset(T->ch,0,MAXLEN);
	T->length=0;
	//Reassign to chars
	strcpy(T->ch,chars);
	T->length=(int)strlen(chars);
	/*printf("len=%d\n",strlen(T->ch));*/
	return 0;
}

/**
 * Empty judgment of string
 */
int StrIsEmptyS(SString T){
	return T.length==0;
}


/**
 * Get string length
 */
int StrLengthS(SString T){
	return T.length;
}

/**
 * Serial connection operation
 */
int StrConcatS(SString* T,char* s1,char* s2){
	//Auxiliary variable
	char *p;
	//Empty contents in T
	memset(T->ch,0,MAXLEN);
	T->length=0;
	//Connection string
	p=strcat(s1,s2);
	//Connect string to T
	strcpy(T->ch,p);
	T->length=(int)strlen(p);
	return 1;
}

/**
 * Intercept substring
 */
int SubStringS(SString* T,SString src,int pos,int len){
	//Clear the original content in T
	memset(T->ch,0,MAXLEN);
	T->length=0;
	//Get substring
	/**
		 char *strncpy(char *dest, const char *src, size_t n)
		 dest -- Points to the target array used to store the copied content
		 src -- String to copy
		 n -- The number of characters to copy from the source
	 */
	strncpy(T->ch,src.ch+pos,len);
	//Reset the length of the string
	T->length=len;
	return 1;
}


/**
 * String comparison
 */
int StrCompareS(SString T1,SString T2){
	/*
		int strcmp(const char *str1, const char *str2)
		If the return value is less than 0, str1 is less than str2
		If the return value is greater than 0, str1 is greater than str2
		If the return value is equal to 0, it means that str1 is equal to str2
	*/
	return strcmp(T1.ch,T2.ch);
}

/**
 * String emptying
 */
int ClearStringS(SString* T){
	memset(T->ch,0,MAXLEN);
	T->length=0;
	return 1;
}

2.3 function test

#include <stdio.h>
#include <stdlib.h>
#include "SString.h"


int main(int argc,char **argv){
	SString str1,str2;
	//str1.ch="Hello,World!";
	StrAssignS(&str1,"Hello,World");
	StrAssignS(&str2,"Hello");
	printf("len=%d\n",StrLengthS(str1));
	//String connection
	StrConcatS(&str1,"SHello","Hi");
	printf("len=%d\n",StrLengthS(str1));
	//Print string content
	printf("str1=%s\n",str1.ch);
	printf("str2=%s\n",str2.ch);
	printf("CompareRes=%d\n",StrCompareS(str1,str2));
	//Intercept substring
	SubStringS(&str2,str1,2,3);
	puts(str1.ch);
	puts(str2.ch);
	//Empty string contents
	ClearStringS(&str1);
	printf("str1's len=%d,str2's len=%d,\nso str1 isEmpty:%d,str2 isEmpty:%d\n",StrLengthS(str1),StrLengthS(str2),StrIsEmptyS(str1),StrIsEmptyS(str2));
	
	return 0;
}

     Paste a screenshot of the test results,

Topics: C C++ data structure