This is a messy code written before the holiday, mainly to solve the problem of string operations, such as string assignment, seeking String Length, seeking substrings, etc.
// # Include "file. h" // # include "function. c" # include
# Include
# Include
# Include
# Include
# Include
// # Include "c1-1.h" using namespace std; typedef struct HString {char * ch; int length;} HString; int StrAssign (HString * T, char * chars) {int I, j; if (T-> ch) free (T-> ch); I = strlen (chars); if (! I) {T-> ch = NULL; T-> length = 0;} else T-> ch = (char *) malloc (I * sizeof (char )); if (! T-> ch) // space allocation failure exit (OVERFLOW); for (j = 0; j
Ch [j] = chars [j]; T-> length = I; return 1;} int Length (HString T) {return T. length;} void InitString (HString * T) {T-> length = 0; T-> ch = NULL;} void StrPrint (HString T) // all the characters after the operation are put to T. {int I; for (I = 0; I
Ch) free (T-> ch); T-> ch = (char *) malloc (t. length) * sizeof (char); if (! T-> ch) exit (OVERFLOW); for (I = 0; I
Ch [I] = t. ch [I]; T-> length = t. length; return 1;} void StrCat (HString * s, HString t, HString r) {int I; if (s-> ch) free (s-> ch ); s-> length = t. length + r. length; s-> ch = (char *) malloc (s-> length * sizeof (char); if (! S-> ch) exit (OVERFLOW); for (I = 0; I
Ch [I] = t. ch [I]; for (I = 0; I
Ch [t. length + I] = r. ch [I];} int SubString (HString * Sub, HString t, int pos, int len) {// sub returns the string with The len Length starting from the pos character in string s; int I; if (pos <1 | pos> t. length | len <0 | len> t. length-pos + 1) exit (OVERFLOW); if (Sub-> ch) free (Sub-> ch); if (! Len) {Sub-> ch = NULL; Sub-> length = 0;} else {Sub-> ch = (char *) malloc (len * sizeof (char )); // if (! Sub-> ch) // exit (OVERFLOW); for (I = 0; I <= len-1; I ++) {Sub-> ch [I] = t. ch [pos-1 + I];} Sub-> length = len;} return 0;} int StrComp (HString t, HString r) {// string comparison if (t. length> r. length) return 1; elsereturn 0;} int StrCompare (HString s, HString t) {// returns the difference int I between the two strings if the two strings are equal; for (I = 0; I
0) {// In fact, we should pay more attention to the legitimacy of the parameters to ensure the robustness of the program. n = Length (s); m = Length (t); I = pos; while (I <= n-m + 1) // note: why can't I determine the value range of the lower mark? {// obtain the string SubString (& sub, s, I, m) with the length of m from the position I of the parent string ); if (StrCompare (sub, t )! = 0) // compare the mode string with the sub-string one by one ++ I; // why is not I ++? Elsereturn I ;}} return 0;} int StrInsert (HString * s, int pos, HString t) {// insert string t, original string s, t is inserted before the pos position of s. // It is a bit faulty when the program is executed. if (pos <1 | pos> s-> length + 1) printf ("the pos position is invalid !!! : "); If (t. length) {s-> ch = (char *) realloc (s-> ch, (s-> length + t. length) * sizeof (char); if (! S-> ch) exit (OVERFLOW); for (I = s-> length-1; I >= pos-1; -- I) s-> ch [I + t. length] = s-> ch [I]; for (I = 0; I
Ch[ pos-1 + I] = t. ch [I]; s-> length = t. length;} return 0;} int StrEmpty (HString s) {if (s. length = 0) printf ("this string is an empty string"); elseprintf ("this string is not an empty string! ");} Void StrDelete (HString * s, int pos, int len) {int I; if (s-> length
Length; I ++) s-> ch [I] = s-> ch [I + len]; s-> length-= len; s-> ch = (char *) realloc (s-> ch, s-> length * sizeof (char);} int StrReplace (HString * s, HString t, HString v) {// replace from the nth position: replace all the non-overlapping substrings in the primary string S that are equal to T with V, int I = 1; // search for the string Tif (StrEmpty (t) printf ("error,") from the first character of string S; do {I = StrIndex (* s, t, i); // The result is that the position of the substring T is found after the previous I. if (I) // The substring S has a string T {StrDelete (s, I, length (t); // Delete the string TStrInsert (s, I, v); // Insert the string Vi ++ = Length (v) at the position of the original string T );}} while (I); return I;} int StrClear (HString * s) {int I = 0;/* while (s-> ch [I]) {free (s-> ch [I]); I ++ ;} s-> length = 0; * // This is the code I wrote at the beginning and cannot give up any learning opportunity while (s-> ch) {free (s-> ch); s-> ch = NULL; // This is easy to forget} s-> length = 0; return 1;} void StrDestory () {} int menu_select () {char c; int n; printf ("\ n"); printf ("implement some functions of string operations! \ N "); printf (""); printf ("\ n *********** YU Bo & Student ID 2012070152 ***** \ n "); printf ("* too many attempts have been made before * \ n "); printf ("㊣ \ n"); printf ("㊣㊣ \ n"); printf ("㊣ \ n "); printf ("initialize failed \ n"); printf ("initialize (1) --- initialize \ n"); printf ("initialize (2) --- string assignment duration \ n "); printf (" duration (3) --- evaluate String Length duration \ n "); printf (" duration (4) --- substring seek \ n "); printf (" Seek (5) --- string connection lead \ n "); printf (" Seek (6) --- string deletion duration \ n "); printf (" duration (7) --- string comparison duration \ n "); print F ("Quit (* 0 *)-exit system quit \ n"); printf ("Quit (* 9 *)-display menu quit \ n "); printf ("character note: currently only letters are supported... * \ n "); printf (" * = select a sequence number and enter: = * ");} int main () {int I, m, n; int flags = 1; char c, p [200]; HString t, s, r, Sub, k,; system ("cls"); InitString (& r); InitString (& a); do {if (flags = 1) menu_select (); scanf ("% d ", & I); switch (I) {// The HString type must be initialized. case 1: {InitString (& t); flags = 0; printf ("Initialization is complete. Please continue with the selection: \ n "); break;} case 2: {print F ("enter a string of less than 200 characters \ n"); cin. get (); // eliminate the effect of line breaks on cin. getline (p, 200); StrAssign (& t, p); // now t has the content printf ("string t:"); StrPrint (t ); printf ("the string assignment is complete. Please continue to select \ n"); flags = 0; // system ("pause"); break;} case 3: {I = Length (t); printf ("string t Length: % d \ n", I); // system ("pause "); printf ("the string length has been completed, please continue to select \ n"); break;} case 4: {printf ("Enter the I character of t, if the length is len, note the parameter range: \ n "); printf (" 1 <= I <= String Length, 0 <= len-I + 1 \ n "); scanf ("% d", & m, & n); printf ("t sub-string is"); Sub String (& Sub, t, m, n); // assume that the length is 5 S from the 5th position of the t String // ubString StrPrint (Sub ); printf ("the substring has been completed, please continue"); flags = 0; // system ("pause"); break;} case 5: {printf ("enter a string, less than 200 characters \ n"); InitString (& s); cin. get (); // eliminate the effect of line breaks on cin. getline (p, 200); StrAssign (& s, p); // now t has the content printf ("string t:"); StrPrint (t ); strCat (& t, t, s); printf ("connected string:"); StrPrint (t); printf ("the substring has been completed. Please continue "); flags = 0; // system ("pause");} case 6: {printf ("Enter the location and length of the deleted string \ N "); printf (" % d ", m, n); StrDelete (& t, m, n); StrPrint (t); flags = 0; break;} case 9: {flags = 1;} // printf ("% d \ ns Length:", I); // I = Length (s ); // printf ("% d \ n comparison s and t:", I);/* I = StrCompare (s, r ); printf ("% d \ n", I); printf ("% d is the first character of string r, which is the same as the first \ n of string s", StrIndex (s, r, 1); printf ("the r string is inserted to the t string from four locations, with the total length of r: \ n"); // StrPrint (t ); // StrPrint (r); StrInsert (& t, 4, r); StrPrint (t); InitString (& k); printf ("Whether the string k is empty "); strEmpty (k); StrAssign (& K, e); printf ("the string e is deleted from the first position. The length is 3, and the remaining string is \ n"); StrReplace (& s, r, t); StrPrint (s); StrClear (& s); printf ("string s is"); StrPrint (s); */} while (I! = 0); return 0 ;}