Summary of data structure related strings in postgraduate entrance examination

Posted by jdh on Wed, 05 Jan 2022 04:25:32 +0100

Summary of data structure related strings in postgraduate entrance examination

1, Basic concept of string

String: it is a finite sequence composed of zero or more characters, marked as: S = 'a1 a2... an' (n ≥ 0)
ai ∈ V character set, each ai (1 ≤ i ≤ n) can be letters, numbers or other characters.
String is also a specific linear table. The logical structure of string is very similar to that of linear table. Its characteristic is that the data object of string is limited to character set.

  • String Name: S
  • String value: a sequence of characters enclosed in single quotes
  • String length: n is the number of characters in the string
  • Null string: the string when n=0
  • Space string (blank string): a string composed of one or more spaces, the length of which is the number of spaces.
  • String Equality: two strings are said to be equal if and only if their values are equal. That is, the two strings are equal only when their lengths are equal and the characters at each corresponding position are equal.
  • Substring: the substring composed of any consecutive characters in the string is called the substring of the string.
  • Main string: a string containing sub strings is called the main string accordingly. It can be seen that the substring is part of the main string.
  • Substring: sub (main string, starting position, length)
    Example: sub ('china ', 2,2) =' in '
    Position of the pattern string in the main string type (pattern matching of the string):
    From the starting position of the main string, the sequence number of the mode string at the position where the main string first appears
    Example: main string 'China' mode string 'in'
    If the starting position is 0, the position of the first character of the mode string in the main string is 2
    If the starting position is 3, the position of the first character of the mode string in the main string is 7

2, Sequential storage representation of strings

Features: use a group of storage units with continuous addresses to store character sequences of string values

//1. Fixed length storage of string 
#define MaxSize 50
typedef struct SString{
	char str[MaxSize+1];
	int length; 
}SString;

//2. Variable length storage of string 
typedef struct HString{
	char *ch;
	int length;
}HString; 

3, Basic operation of sequence string

1. Initialization of string
  • Objective: initialize an empty string S.
#define OVERFLOW 0
#define OK 1
#define InitSize 50
//3. Initialization of sequence string
int InitString(HString &S){
	S.ch = (char *)malloc(sizeof(char)*InitSize);
	if(!S.ch)
		exit(OVERFLOW);
	S.length = 0;
	return OK;
} 
2. Find the string length
  • Target: return the length of the current string (excluding the length of '\ 0')
//4. Find the string length
int SteLength(HString S){
	return S.length;
} 
3. String insertion
  • Target: insert string T before (including the tail) the pos character of string S
#define ERROR 0
#define TRUE 1
//5. Basic operation of string insertion
int StrInsert(HString &S,int pos,HString T){
	if(pos<1||pos>S.length+1)
		return ERROR;
	if(T.length){
		S.ch = (char *)realloc(S.ch,(S.length+T.length)*sizeof(char));
		if(!(S.ch))
			exit(OVERFLOW);
		for(i=S.length-1;i>=pos-1;i--)
			S.ch[i+T.length] = S.ch[i];
		for(i=0;i<T.length;i++)
			S.ch[i+pos-1] = T.ch[i];
		S.length+=T.length;
	}
	return TRUE;
} 

4. String comparison
  • Objective: compare the sizes of strings S and T. If s > t, it returns a positive number, if s < T, it returns a negative number, and if S=T, it returns 0.
//5. String comparison
int StrCompare(HString S,HString T){
	for(int i=0;i<S.length&&i<T.length;i++)
		if(S.ch[i]!=T.ch[i])
			return S.ch[i]-T.ch[i];
	return S.length-T.length;
} 

5. String assignment (string assignment)

Target: assign the string pointed to by the current chars pointer to T. If there is data in T, clear it and then assign it.

//6. String assignment (string assignment)
int StrAssign(HString &T,char *chars){
	if(T.ch)
		free(T.ch);
	char *c;
	int i;
	for(i=0,c=chars;*c!='\0';++i,++c);
	if(!i){
		T.ch = NULL;
		T.length = 0;
	}
	else{
		if(!(T.ch=(char *)malloc(i*sizeof(char))))
			exit(OVERFLOW);
		for(int j=0;j<i;j++)
			T.ch[j]=chars[j]; //chars[j] is equivalent to * c and * chars 
			T.length = i;
	}
	
	return TRUE;
	
} 

6. Substring
  • Objective: copy the character sequence with length len from the pos character in string S to string Sub.
//7. Substring
int GetSubString(HString &SubStr,HString S,int pos,int len){
	if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
		return ERROR;
	if(SubStr.ch){
		free(SubStr.ch);
		SubStr.ch = NULL;
	}
	if(len==0){
		SubStr.ch=NULL;
		SubStr.length=0;
		return TRUE;
	}
	else{
		SubStr.ch = (char *)malloc(sizeof(char)*(len+1));
		for(int i=pos-1,j=0;i<pos+len;i++,j++)
			SubStr.ch[j] = S.ch[i];
		SubStr.ch[len] = '\0';
		SubStr.length = len;
		return TRUE;
	}
		
		 
} 

4, Complete code

#include<iostream>
#include<stdlib.h>
using namespace std;

//1. Fixed length storage of string 
#define MaxSize 50
typedef struct SString{
	char str[MaxSize+1];
	int length; 
}SString;

//2. Variable length storage of string 
typedef struct HString{
	char *ch;
	int length;
}HString; 

#define OVERFLOW 0
#define OK 1
#define InitSize 50
//3. Initialization of sequence string
int InitString(HString &S){
	S.ch = (char *)malloc(sizeof(char)*InitSize);
	if(!S.ch)
		exit(OVERFLOW);
	S.length = 0;
	return OK;
} 
 
//4. Find the string length
int SteLength(HString S){
	return S.length;
} 

#define ERROR 0
#define TRUE 1
//5. Basic operation of string insertion
int StrInsert(HString &S,int pos,HString T){
	if(pos<1||pos>S.length+1)
		return ERROR;
	if(T.length){
		S.ch = (char *)realloc(S.ch,(S.length+T.length)*sizeof(char));
		if(!(S.ch))
			exit(OVERFLOW);
		for(int i=S.length-1;i>=pos-1;i--)
			S.ch[i+T.length] = S.ch[i];
		for(int i=0;i<T.length;i++)
			S.ch[i+pos-1] = T.ch[i];
		S.length+=T.length;
	}
	return TRUE;
} 

//5. String comparison
int StrCompare(HString S,HString T){
	for(int i=0;i<S.length&&i<T.length;i++)
		if(S.ch[i]!=T.ch[i])
			return S.ch[i]-T.ch[i];
	return S.length-T.length;
} 

//6. String assignment (string assignment)
int StrAssign(HString &T,char *chars){
	if(T.ch)
		free(T.ch);
	char *c;
	int i;
	for(i=0,c=chars;*c!='\0';++i,++c);
	if(!i){
		T.ch = NULL;
		T.length = 0;
	}
	else{
		if(!(T.ch=(char *)malloc(i*sizeof(char))))
			exit(OVERFLOW);
		for(int j=0;j<i;j++)
			T.ch[j]=chars[j]; //chars[j] is equivalent to * c and * chars 
			T.length = i;
	}
	
	return TRUE;
	
} 

//7. Substring
int GetSubString(HString &SubStr,HString S,int pos,int len){
	if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
		return ERROR;
	if(SubStr.ch){
		free(SubStr.ch);
		SubStr.ch = NULL;
	}
	if(len==0){
		SubStr.ch=NULL;
		SubStr.length=0;
		return TRUE;
	}
	else{
		SubStr.ch = (char *)malloc(sizeof(char)*(len+1));
		for(int i=pos-1,j=0;i<pos+len;i++,j++)
			SubStr.ch[j] = S.ch[i];
		SubStr.ch[len] = '\0';
		SubStr.length = len;
		return TRUE;
	}
		
		 
} 
int main(){
	return 0;
}

Topics: data structure string