Sequential storage representation of String Length

Source: Internet
Author: User

Sequential storage representation of String Length

The sequential storage of strings is a sequential storage structure similar to a linear table. It uses a set of sequential storage units to store string values.

The following functions are implemented:

Int Length (SString s); // String Length

Bool StringConcat (SString T, SString s1, SString s2); // return the string connected to s1 and s2 with T

Void PrintString (char T []); // output

Bool StringAssign (SString T, const char str []); // generate a string with the str Value

Bool StrCopy (SString T, SString s); // copy s to T

Bool StrEmpty (SString T); // empty

Int StrCompare (SString T, SString T1); // comparison

Void ClearString (SString T); // clear

Bool SubString (SString T, SString S, int pos, int len); // returns a SubString.

Bool StrInsert (SString S, int pos, SString T); // insert

Bool StrDelete (SString S, int pos, int len); // Delete

 

However, I still do not know the Replace () and Index () functions, so I will not write them here.

 

The specific code is as follows:

Header file:

 

# Pragma once # include
 
  
Using namespace std; # define MAX_STRING_SIZE 8 typedef char SString [MAX_STRING_SIZE + 1]; int Length (SString s); // String Length bool StringConcat (SString T, SString s1, SString s2); // use T to return the void PrintString (char T []) connected to s1 and s2; // output bool StringAssign (SString T, const char str []); // generate the str string bool StrCopy (SString T, SString s); // copy s to Tbool StrEmpty (SString T ); // empty int StrCompare (SString T, SString T1); // compare void ClearString (SString T); // clear bool SubString (SString T, SString S, int pos, int len); // return the substring bool StrInsert (SString S, int pos, SString T); // insert bool StrDelete (SString S, int pos, int len ); // Delete
 


 

Function declaration:

 

# Include "String. h "int Length (SString s) {return strlen (s);} bool StringConcat (SString T, SString s1, SString s2) {int t_length = MAX_STRING_SIZE; int s1_length = Length (s1); int s2_length = Length (s2); if (t_length> = s1_length + s2_length) {for (int I = 0; I
 
  
S1_length & t_length
  
   
T1 [j]-'0') {return 1;} else if (T [I]-'0' = T1 [j]-'0 ') {return 0;} else {return-1 ;}} exit (-1) ;}void ClearString (SString T) {T [0] = '\ 0 ';} bool SubString (SString T, SString S, int pos, int len) {if (pos> Length (S) | pos <1 | len <0) {return false ;} else if (len> Length (S)-pos + 1) {cout <"unable to return, returns the original string:"; for (int I = pos-1; I
   
    
Length (S) + 1) {return false;} else {for (int I = Length (S); I >= pos-1; -- I) {S [I + Length (T)] = S [I];} for (I = 0; I <Length (T); ++ I) {S [pos + I-1] = T [I];} return true;} bool StrDelete (SString S, int pos, int len) {if (pos <0 | pos> Length (S) | len <0) {return false;} else if (pos> Length (S)-len + 1) {S [pos-1] = '\ 0';} else {for (int I = pos + len; I <= Length (S)-1; ++ I) {S [I-len] = S [I];} S [Length (S)-len] = '\ 0';} return true ;}
   
  
 


 

Test file:

 

# Include "String. h "void main () {SString T; char str [] =" bit "; SString s1 =" hello "; SString s2 =" world "; StrInsert (s1, 2, s2); PrintString (s1); SubString (T, s1, 1, 6); PrintString (T); StringConcat (T, s1, s2); PrintString (T); StrCopy (T, s1); PrintString (T); StringAssign (T, str); PrintString (T); StrDelete (s2, 1, 1); PrintString (s2); StrEmpty (T ); if (StringAssign (s1, s2) = 1) {cout <"the first string is greater than the last string" <
 
  

 

I hope you can point out my shortcomings. Thank you ~ I will try to improve it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.