Data structure-string-complete executable code __ data structure

Source: Internet
Author: User
Tags truncated
#include "stdio.h" #include "string.h" #include "stdlib.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/typedef char STRING[MAXSIZE+1];
	/* Unit NO. 0 Storage String Length * * * output string t/F Strprint (String t) {int i;
	for (i=1;i<=t[0];i++) printf ("%c", T[i]);
printf ("\ n");
	/* If S is an empty string, returns True, otherwise returns false */Status Strempty (String S) {if (s[0]==0) return true;
else return FALSE;
	/* * Generates a string T/Status strassign (String T,char *chars) with value equal to 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;

}/////* Returns the number of elements of the string */int strlength (String S) {return s[0];} /* Initial Condition: string S and T exist////////////////////////////////////////////////////////////
	NT 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]; }/* with TReturns a new string of S1 and S2 joins.
	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 the substring of 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 POS character in the main string s. If it does not exist, the function returns a value of 0. * */* where, T is not empty, 1≤pos≤strlength (S).				/int Index (string s, String T, int pos) {int i = pos;/* I is used in the main string S in the current position subscript value, if the POS is not 1, then starting from the POS position match * * Int j = 1; * * J for substring T in the current position subscript/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 letter is equal to continue * *
			++i;
      	++j;		else/* Pointer back to start match/{i = i-j+2; 			/* I back to the last match first of the next * * j = 1;
	/* J returns to substring T's first/}} if (J > t[0]) return i-t[0];
else return 0; }/* Initial condition: string s and T exist, 1≤pos≤strlength (s) +1///////////////////* Operation result: Insert T before ordinal 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) {/* Fully 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 {//* part Insert */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 conditions: string s exist, 1≤pos≤strlength (s)-len+1//////////////////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 non-null (this function has nothing to do with the storage structure of the string)/////////////////* Operation result: Replace all the non-overlapping substrings in the main string S with V/Status replace string s,string t,string
	V) {int i=1;/* Find string T/if (Strempty (t)) from the first character of string s * * * is an empty string/return ERROR; do {i=index (s,t,i);/* result I is the position of the substring T found after the last I/if (i)/* string S in the presence of a string t/{strdelete (S,i,strlength (T)); * Delete the string T * /Strinsert (S,I,V); /* Inserts a string V/i+=strlength (v) at the position of the original string T;
	/* Continue to find the string t/}}while after inserting the string V (i);
return OK;
    int main () {int I, J, Opp, POS;
	char S, str;
	String t,s1,s2,sub;

    Status K; printf ("\n1. Strassign generate string \n2. Strlength for the string long \n3.
    Strcompare string comparison "); printf ("\n4. Concat string Connection \n5. SubString to find the substring \n6.
    Index to find substring position "); printf ("\n7. The Strinsert substring is inserted into the \n8. Strdelete the substring to delete the \n9.
    Replace substring substitution ");
    printf ("\n0. Exit \ n" Please select your action: \ n ");
        while (opp!= ' 0 ') {scanf ("%d", &opp);
            Switch (OPP) {case 1:k=strassign (S1, "nowamagic.net"); if (!k) {printf("String length exceeds MAXSIZE (=%d) \ n", MAXSIZE);
            Exit (0);
            printf ("string S1:");
            Strprint (S1);
            printf ("\ n");

        Break
            Case 2:printf ("string s1 length%d \ n", Strlength (S1));

        Break
            Case 3:k=strassign (S2, "google.com");
                if (!k) {printf ("string length exceeds MAXSIZE (%d) \ n", MAXSIZE);
            Exit (0);
            printf ("String s2:");
            Strprint (S2);

            printf ("\ n");
            I=strcompare (S1,S2);
            if (I < 0) s= ' < ';
            else if (i==0) s= ' = ';
            else s= ' > ';
            printf ("String s1%c string s2\n", s);

        Break
            Case 4:concat (T,S1,S2);
            Strprint (t);

        Break
            Case 5:printf ("substring of string S1, enter the starting position of the substring:");
            scanf ("%d", &i);
            printf ("Please enter the length of the substring:"); scanf ("%d",; j);
            printf ("Starting position:%d, substring length:%d\n", I, j);
            K=substring (SUB,S1,I,J);
                if (k) {printf ("substring sub is:");
            Strprint (sub);

        } break;
            Case 6:printf ("main string S1:");
            Strprint (S1);
            K=strassign (Sub, "Magic");
            printf ("Substring sub is:");
            Strprint (sub);
            I=index (s1,sub,1);
            printf ("S1 the first%d letter and sub match \ n", i);

        Break
            Case 7:printf ("main string S1:");
            Strprint (S1);
            K=strassign (Sub, "LOL");
            printf ("Substring sub is:");
            Strprint (sub);
            printf ("Please enter the location to insert:");
            scanf ("%d", &pos);
            Strinsert (s1,pos,sub);
            Strprint (S1);

        Break
            Case 8:printf ("from the first POS character of string s1, delete len character, please enter POS: \ n");
            scanf ("%d", &i);
            printf ("Enter Len: \ n");
            scanf ("%d", &j); StrdelEte (S1,I,J);
            Strprint (S1);

        Break
            Case 9:printf ("main string S1:");
            Strprint (S1);
            Strassign (T, "a");
            printf ("String T is:");
            Strprint (t);
            Strassign (Sub, "AA");
            printf ("String sub is:");
            Strprint (sub);
            printf ("string s2 replaces string s1 and string t the same non-overlapping string, S1 as:");
            Replace (s1,t,sub);
            Strprint (S1);

        Break
        Case 0:exit (0); }
    }
}



Learning on the road, and June mutual encouragement.

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.