#include "string.h"
#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 40/* Storage space Initial allocation */
typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */
typedef int elemtype;/* Elemtype Type is based on the actual situation and is assumed to be int */
typedef char STRING[MAXSIZE+1]; /* Unit No. 0 The length of the string */
/* Generate a string whose value equals chars t/*
Status strassign (String t,char *chars)
{
int i;
if (strlen (chars) >maxsize)
return ERROR;
Else
{
T[0]=strlen (chars);
for (i=1;i<=t[0];i++)
t[i]=* (chars+i-1);
return OK;
}
}
/* Copy the string t by string S */
Status strcopy (String t,string S)
{
int i;
for (i=0;i<=s[0];i++)
T[i]=s[i];
return OK;
}
/* If S is an empty string, returns True, otherwise false */
Status strempty (String S)
{
if (s[0]==0)
return TRUE;
Else
return FALSE;
}
/* Initial conditions: string s and t exist */
/* Operation Result: If s>t, the return value >0; if s=t, the return value = 0; if s<t, the return value <0 */
int Strcompare (String s,string T)
{
int i;
for (I=1;i<=s[0]&&i<=t[0];++i)
if (S[i]!=t[i])
return s[i]-t[i];
return s[0]-t[0];
}
/* Returns the number of elements of a string */
int Strlength (String S)
{
return s[0];
}
/* Initial Condition: string s exists. Operation Result: Clear S to empty string */
Status clearstring (String S)
{
s[0]=0;/* string length is zero */
return OK;
}
/* Returns a new string of S1 and S2 joined with T. Returns true if not truncated, otherwise false */
Status Concat (String t,string s1,string S2)
{
int i;
if (s1[0]+s2[0]<=maxsize)
{/* Not truncated */
for (i=1;i<=s1[0];i++)
T[i]=s1[i];
for (i=1;i<=s2[0];i++)
T[s1[0]+i]=s2[i];
T[0]=S1[0]+S2[0];
return TRUE;
}
Else
{/* truncate S2 */
for (i=1;i<=s1[0];i++)
T[i]=s1[i];
for (i=1;i<=maxsize-s1[0];i++)
T[s1[0]+i]=s2[i];
T[0]=maxsize;
return FALSE;
}
}
/* Use sub to return the first POS character of string s with a substring of length len. */
Status SubString (String sub,string s,int pos,int len)
{
int i;
if (pos<1| | pos>s[0]| | len<0| | LEN>S[0]-POS+1)
return ERROR;
for (i=1;i<=len;i++)
SUB[I]=S[POS+I-1];
Sub[0]=len;
return OK;
}
/* Returns the position of the substring T after the first POS character in the main string s. If it does not exist, the function returns a value of 0. */
/* where, t non-empty, 1≤pos≤strlength (S). */
int Index (string S, string T, int pos)
{
int i = pos;/* I is used for the current position subscript value in the main string s and, if POS is not 1, matches from POS position */
Int j = 1;/* J for the current position subscript value in the substring T */
while (i <= s[0] && J <= t[0])/* If I is less than the length of S and J is less than the length of T, the Loop continues */
{
if (s[i] = = T[j]) /* Two letters equal then continue */
{
++i;
++j;
}
Else//pointer back to restart match */
{
i = i-j+2;/* I return to the next last match first */
J = 1;/* J return to the first of the substring T */
}
}
If (J > T[0])
return i-t[0];
Else
return 0;
}
/* t is a non-empty string. If there is a substring equal to T after the POS character in the main string S, */
/* Returns the position of the first such substring in S, otherwise returns 0 */
int Index2 (string S, string T, int pos)
{
int n,m, I
String Sub;
if (pos > 0)
{
N = strlength (S);/* Gets the length of the main string S */
M = strlength (t);/* Gets the length of the substring T */
i = pos;
while (i <= n-m+1)
{
SubString (sub, S, I, M);/* Take the sub-string with the length of the first I position in the main string to sub */
if (Strcompare (sub,t)! = 0)/* If two String Unequal */
++i;
else/* If two strings equal */
Return i;/* return i value */
}
}
return 0;/* If no substring is equal to T, return 0 */
}
/* Initial conditions: string S and T exist, 1≤pos≤strlength (s) +1 */
/* Operation result: Insert string T before the first POS character of string S. Full Insert returns True, partial insert returns false */
Status Strinsert (String s,int pos,string T)
{
int i;
if (pos<1| | POS>S[0]+1)
return ERROR;
if (s[0]+t[0]<=maxsize)
{/* Full insert */
for (i=s[0];i>=pos;i--)
S[i+t[0]]=s[i];
for (i=pos;i<pos+t[0];i++)
S[I]=T[I-POS+1];
S[0]=S[0]+T[0];
return TRUE;
}
Else
{/* Partially inserted */
for (i=maxsize;i<=pos;i--)
S[i]=s[i-t[0]];
for (i=pos;i<pos+t[0];i++)
S[I]=T[I-POS+1];
S[0]=maxsize;
return FALSE;
}
}
/* Initial Condition: string S presence, 1≤pos≤strlength (s)-len+1 */
/* Operation Result: Remove the first POS character from string s a substring of length Len */
Status strdelete (String s,int pos,int len)
{
int i;
if (pos<1| | pos>s[0]-len+1| | LEN<0)
return ERROR;
for (i=pos+len;i<=s[0];i++)
S[i-len]=s[i];
S[0]-=len;
return OK;
}
/* Initial conditions: string s,t and V exist, T is a non-empty string (This function is independent of the storage structure of the string) */
/* Operation result: Replace all occurrences of the non-overlapping substring in the main string s with the T equal to V */
Status Replace (String s,string t,string V)
{
int i=1; /* Find string T from the first character of string S */
if (Strempty (T))/* T is an empty string */
return ERROR;
Do
{
I=index (S,t,i); /* result I is the location of the substring T found after the previous I */
if (i)/* string S is present in string T */
{
Strdelete (S,i,strlength (T)); /* Delete the string T */
Strinsert (S,I,V); /* Insert string V at the original string T */
I+=strlength (V); /* Continue to look for string T after inserted string V */
}
}while (i);
return OK;
}
/* Output string t/*
void Strprint (String T)
{
int i;
for (i=1;i<=t[0];i++)
printf ("%c", T[i]);
printf ("\ n");
}
int main ()
{
int i,j;
Status K;
char s;
String t,s1,s2;
printf ("Please enter string S1:");
K=strassign (S1, "ABCD");
if (!k)
{
printf ("String length exceeding MAXSIZE (=%d) \ n", MAXSIZE);
Exit (0);
}
printf ("String length is%d string null no?") %d (1: Yes 0: NO) \ n ", Strlength (S1), Strempty (S1));
Strcopy (S2,S1);
printf ("Copy-S1-generated string:");
Strprint (S2);
printf ("Please enter string s2:");
K=strassign (S2, "Efghijk");
if (!k)
{
printf ("String length exceeding MAXSIZE (%d) \ n", MAXSIZE);
Exit (0);
}
I=strcompare (S1,S2);
if (i<0)
S= ' < ';
else if (i==0)
s= ' = ';
Else
S= ' > ';
printf ("String s1%c string s2\n", s);
K=concat (T,S1,S2);
printf ("string S1 join string s2 The resulting string T is:");
Strprint (t);
if (K==false)
printf ("string t has truncation \ n");
Clearstring (S1);
printf ("Qing is empty string, string S1 is:");
Strprint (S1);
printf ("String length is%d string null no?") %d (1: Yes 0: NO) \ n ", Strlength (S1), Strempty (S1));
printf ("To find the substring of string T, enter the starting position of the substring, substring length:");
i=2;
j=3;
printf ("%d,%d \ n", i,j);
K=substring (S2,T,I,J);
if (k)
{
printf ("substring s2:");
Strprint (S2);
}
printf ("Remove Len characters from the POS character of String T, enter Pos,len:");
i=4;
j=2;
printf ("%d,%d \ n", i,j);
Strdelete (T,I,J);
printf ("The deleted string T is:");
Strprint (t);
I=strlength (S2)/2;
Strinsert (s2,i,t);
printf ("string s2 is: \ n", i) after inserting a string t before the string S2%d characters;
Strprint (S2);
I=index (s2,t,1);
printf ("S2 of%d letters and T first match \ n", i);
SubString (t,s2,1,1);
printf ("String T is:");
Strprint (t);
Concat (s1,t,t);
printf ("string S1:");
Strprint (S1);
Replace (S2,T,S1);
printf ("String S1 instead of string s2 and string t the same non-overlapping string, S2 is:");
Strprint (S2);
return 0;
}
C-language implementation of data structure of string