Data structure -- basic operation of string (C language)

Posted by lur on Sun, 26 Dec 2021 07:50:12 +0100

strand

preface

This paper introduces the programming implementation of the basic operation of string in data structure, and master the basic operations such as string establishment, traversal, substring and positioning

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is a string?

In computer programming, string is usually a character sequence, either a literal constant or a variable. The latter may allow its elements to mutate and change in length, Or fixed (after creation). A string is usually regarded as a data type, usually implemented as an array data structure of bytes (or words). It stores a series of elements, usually characters encoded with some characters. A string can also be used to represent more general array or other sequence (or list) data types and structures.

Depending on the programming language used and the precise data type, variables declared as strings may cause the storage in memory to be statically allocated to a predetermined maximum length, or dynamic allocation can be used to accommodate a variable number of elements.

When a string appears in the source code, it is called a string literal or an anonymous string.

In the formal language used in mathematical logic and theoretical computer science, string is a limited sequence of symbols selected by a set called the alphabet.
There are two implementation forms of string, heap allocation and block chain storage. This paper describes block chain storage

2, How to understand string?

String sting, also known as string, is a finite sequence of 0 or more characters. Generally, we use S = "a1 a2 a3... an", where S is the name of the string, double quotation marks or single quotation marks are used as the delimiter of the string to represent the content of the string, that is, the string value, AI (0 < = I < = n) represents a single element in the string, and N represents the number of threads, that is, how many characters are in the string. When n is 0, This string is called a null string, which is represented by double quotation marks "" and the symbol is ะค.

Note: there is a difference between a blank string and an empty string. A blank string is a string composed of one or more spaces.
In JAVA, String has a deeper understanding. This article will not describe it too much

3, Forms of various strings

subString: a subString composed of any number of consecutive characters in the string is the subString of the string

Main string: accordingly, the string containing the substring is called the main string

index of substring: when the substring first appears in the main string, the first character of the substring corresponds to the serial number in the main string, that is, the position of the substring in the main string.

Here is A classic example to illustrate. For example, let A and B be A = 'This is a string' and B = 'is' respectively, then B is the substring of A and A is the main string. B appears twice in A, of which the main string position corresponding to the first occurrence is 3. Therefore, the position of B in A is called 3. In particular, an empty string is A substring of an arbitrary string, and an arbitrary string is its own substring.

Concatenation: concatenation is an important binary operation. For the substrings s and T in any two main strings, their connection determines the symbol sequence according to the sequence before and after placing s and t. for example, if the substring s = love, t = Hu, then st is lovehu and ts is hugove.

prefixes and suffixes: the string s can be said to be the prefix of T. If there is a string u that satisfies the condition t =su. If u is not empty, then s is an appropriate prefix of T; Accordingly, the string s can be said to be the suffix of t if there is a string u that satisfies the condition t=us. If u is not empty, s can be said to be an appropriate suffix of T. prefixes and suffixes can be said to be substrings of T.

Rotation: String s = uv can be said to be the rotation of t if t = vu For example, when u = 00110 and V = 01, string 0011001 is the rotation of 0100110.

Reversal: the reversal of a string is a string with the same symbols in reverse order. For example, if s=abc (a, b and c are symbols in the alphabet), the reversal of S is cba. A string opposite to itself (for example, s=madam, reversal or madam) is called palindrome palindrome, which also includes empty strings and all strings with length of 1.

4, Implementation of string

1. Implementation of string

1.1 import and storage and setting structure

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define CHUNKSIZE 80

typedef struct 
{
	char *ch;
	int length;
}HString;

1.2 initialization string

int StrAssign(HString &T,char *chars)//Generates a T whose value is equal to the string constant chars 
{
//	if(T.ch) free(T.ch);
	int i,n;char *c;
	for(i=0,c=chars;*c;++i,++c);
	if(!i)
	{
		T.ch=NULL;
		T.length=0;
	}
	else
	{
		if(!(T.ch=(char *)malloc(i*sizeof(char))))
			return -1;
		for(n=0;n<=i-1;n++)
		{
			T.ch[n]=chars[n];
			T.length=i;
		}
	}
	return 1;
}

1.3 function realization of substring

 int SubString(HString &Sub,HString S,int pos,int len)//substring  
 {
 	int n; 
 	if(pos<0||pos>S.length||len<0||len>S.length-pos+1)
 	{
 		 		return 0;
	 }
//	if(Sub.ch) free(Sub.ch);
	if(!len)
		{
			Sub.ch=NULL;
			Sub.length=0;
		 } 
	else
		{
			Sub.ch=(char *)	malloc(len * sizeof(char));
			for(int n=0;n<=len-1;n++)
			{
				Sub.ch[n]=S.ch[pos+n-1];
			}
			Sub.length=len;
		}
	return 1;
 }

1.4 function realization of two string comparison

 int StrCompare(HString S,HString T)//If s > t, the return value is > 0; If S=T, the return value is 0; If s < T, the return value is < 0 
 {
 	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];
		else
			  return S.length-T.length;
	 }	
 }

1.5 positioning function

int Index(HString S,HString T,int pos)
{
	int i=pos,j=0;
	while(i<S.length&&j<T.length)
	{
		if(S.ch[i]==T.ch[j])
		{
			++i;++j;
		}
		else
		{
			i=i-j+1;
			j=0;
		}
		
	}
	if(j>=T.length) return i-T.length;
	else return 0;
}

1.6 length function of string

int StrLength(HString T)//Find string length 
{
	return T.length;
 } 

1.7 output function of string

void StrPrint(HString T)
{
	int i;
	for(i=0;i<T.length;i++)
	{
		printf("%c",T.ch[i]);
	}
} 

1.7 the main function realizes all the above functions

void Show()
{
	printf("Please enter the command you want to select:\n");
	printf("1.Display string\n");
	printf("2.Display string length\n");
	printf("3.Compare the two strings\n");
	printf("4.substring \n");
	printf("5.location\n");
	printf("0.sign out\n");
	printf("-------------------\n");
}

int main()
{
	int n;
	Show();	
	scanf("%d",&n);	
	char s[15]="qwertasdfzxc";		
	HString S;
	StrAssign(S,s);	
	char t[5]="asdf";
	HString T;
	StrAssign(T,t);	
	HString Sub;
	while(n!=0)
	{
		switch(n)
		{
			case 1:	
					printf("The first string is:%s\n",S.ch);
					printf("The second string is:%s\n",T.ch);break;
			case 2: printf("Article 1 string length:%d\n",StrLength(S));
					printf("Article 2 string length:%d\n",StrLength(T));break;
			case 3: 	int n ;
						n=StrCompare(S,T);
						if(n!=0)
							if(n>0)
								printf("Front length\n");
							else
								printf("Back length\n");
						else
							printf("Same length\n");break; 
			case 4:printf("Please enter the position and length you want to take:\t");
					int j,l;
					scanf("%d %d",&j,&l);
					SubString(Sub,S,j,l);
					StrPrint(Sub);break;
			case 5: int k;
					printf("Enter the location of the query:\n");
					scanf("%d",&k);
					int x=Index(S,T,k);
				    printf("The location to locate is:%d",x);break;
		 } 
			printf("\n");
			Show();
			scanf("%d", &n);
		}
	return 0;
 } 

summary

The above is what we want to talk about today. This paper only briefly introduces the string infrastructure and the corresponding implementation of basic functions.
I hope it will help you

Topics: data structure string