_datastructure_c_impl: Sequential string

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #define MAXLENGTH 60typedef struct{char str[maxlength];int length;} Assignment operation of seqstring;//string void Strassign (seqstring *s,char cstr[]) {int i;for (i=0;cstr[i]!= ' n '; i++) s->str[i]=cstr[i];// Assigned the value of the character in the constant CStr to the string ss->length=i;} Determines whether the string is empty, the string is null to return 1, otherwise returns 0int Strempty (Seqstring S) {if (s.length==0) return 1;elsereturn 0;} Find the length of the string operation int strlength (seqstring S) {return s.length;} The copy operation of the string void Strcopy (seqstring *t,seqstring S) {int i;for (i=0;i<s.length;i++)//will string S Word assigned value to the string tt->str[i]=s.str[i]; t->length=s.length;//the length of the string S to the string t}//string comparison operation int Strcompare (seqstring s,seqstring T) {//Compare the characters in two strings int i;for (i=0;i< s.length&&i<t.length;i++) {//compares characters in two strings if (S.str[i]!=t.str[i])//Returns a difference of two characters if the characters are different (s.str[i]- T.str[i]);} Return (s.length-t.length), or//If the comparison is complete, returns the difference of the length of the two string}//the insert operation of the string. Inserting T in the POS position in S is divided into three cases of int strinsert (seqstring *s,int pos,seqstring t) {int i;if (pos<0| | Pos-1>s->length) {//the insertion position is incorrect, return 0printf ("Incorrect insertion position"); return 0;} if (s->length+t.length<=maxlength) {//First case, insert substring after string long ≤maxlength, that is, substring T is inserted into string S in for (i=s->length+t.length-1;i>=pos+t.length-1;i--)//before inserting a substring t, Move the character after Pos in S to the back of Len position s->str[i]=s->str[i-t.length];for (i=0;i<t.length;i++)//Insert the string into S s->str[pos+i-1]= T.str[i]; S->length=s->length+t.length;return 1;} else if (pos+t.length<=maxlength) {///In the second case, the substring can be fully inserted into s, but the characters in s will be truncated for (i=maxlength-1;i>t.length+pos-i;i--)/ /Move the post-Pos character in S to the last s->str[i]=s->str[i-t.length];for of the array (i=0;i<t.length;i++)//insert T into S s->str[i+pos-1] =t.str[i]; S->length=maxlength;return 0;} else{//in the third case, the substring T cannot be fully inserted into S, the character in T will be discarded for (i=0;i<maxlength-pos;i++)//t is inserted directly into S, before inserting the characters in s that do not need to be moved s->str[i+ Pos-1]=t.str[i]; S->length=maxlength;return 0;}} Delete pos start in string S len character int strdelete (seqstring *s,int pos,int len) {int i;if (pos<0| | len<0| | Pos+len-1>s->length) {printf ("Incorrect delete location, parameter len not valid"); return 0;} Else{for (i=pos+len;i<=s->length-1;i++) s->str[i-len]=s->str[i]; S->length=s->length-len;return 1;}} string connection operation int StrCat (seqstring *t,seqstring S) {int I,flag;if (T->length+s.length<=maxlength) {for (i=t->length;i<t->length+s.length;i++) T->str[i]= s.str[i-t->length]; T->length=t->length+s.length;flag=1;} else if (t->length<maxlength) {for (i=t->length;i<maxlength;i++) t->str[i]=s.str[i-t->length]; t->length=maxlength;flag=0;} return flag;} Intercept substring operation int SubString (seqstring *sub,seqstring s,int pos,int len) {int i;if (pos<0| | len<0| | Pos+len-1>s.length) {printf ("parameter pos and Len not valid"); return 0;} Else{for (i=0;i<len;i++) sub->str[i]=s.str[i+pos-1]; Sub->length=len;return 1;}} The positioning operation of the string int strindex (seqstring s,int pos,seqstring t) {int i,j;if (Strempty (T)) return 0;i=pos;j=0;while (i<s.length &&j<t.length) {if (S.str[i]==t.str[j]) {i++;j++;} else{i=i-j+1;j=0;}} if (j>=t.length) return i-j+1;else return 0;} The substitution operation of the string int strreplace (seqstring *s,seqstring t,seqstring V) {//Replaces all T in S with Vint I;int flag;if (Strempty (T)) return 0;i=0; Do{i=strindex (*s,i,t);//Locate T in S position if (i) {strdelete (S,i,strlength (T));//delete found Tflag=sTrinsert (s,i,v);//Insert Vif (!flag) return 0;i+=strlength (V) at I position;}} while (i); return 1;} Empty operation of the string void Strclear (Seqstring *s) {s->length=0;} ===========void strprint (seqstring S) {int i;for (i=0;i<s.length;i++) {printf ("%c", S.str[i]);} printf ("\ n");} void Main () {seqstring S1,s2,sub;char ch[maxlength];p rintf ("Please enter first string: \ n"); gets (CH); Strassign (&s1,ch);p rintf ("Output string S1:"); Strprint (S1);p rintf ("Please enter a second string: \ n"); gets (CH); Strassign (&s2,ch);p rintf ("Output string S2:"); Strprint (S2);p rintf ("Insert the string S2 into the 13th position of the S1: \ n"); Strinsert (&AMP;S1,13,S2); Strprint (S1);p rintf ("Remove the 7 characters from the 22nd position in the string S1: \ n"); Strdelete (&s1,22,7); Strprint (S1);p rintf ("Remove the 4 characters from the 6th position in the string S2 into a sub: \ n"); SubString (&sub,s2,6,4); Strprint (sub);p rintf ("assigns a string Sub to america:\n"); Strassign (&sub, "America");p rintf ("Replace the string S1 in the string S2 with a Sub: \ n"); Strreplace (&s1,s2,sub); Strprint (S1); System ("Pause");}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

_datastructure_c_impl: Sequential string

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.