Data Structure Learning (10)-string operations

Source: Internet
Author: User

I haven't read the data structure for a few days. Today I started again.

A string is a special linear table. Each node of a string is a character. Therefore, a string is also called a string.

The operations on strings mainly include determining the length of strings, copying strings, connecting strings, seeking substrings, inserting strings, deleting strings, and locating substrings. String operations are also a common part of the C language test.

The following code implements the main operations on strings.

# Include <stdio. h> # include <stdlib. h> # define maxsize 20 char * string_create (); // create the int string_length (char * s); // obtain the string length void string_show (char * s ); // output string char * string_copy (char * D, char * s); // string copy char * string_connect (char * D, char * s ); // string connection char * string_substr (char * D, char * s, int POs, int Len); // obtain the substring int string_compare (char * D, char * s ); // string comparison char * string_insert (char * D, char * s, int POS); // string insertion char * string_del Ete (char * D, int POs, int Len); // Delete int string_index (char * D, char * s, int POS ); // String Match int main (void) {int choice; char * STR, * C; int ans, POs, Len; C = (char *) malloc (sizeof (maxsize); printf ("enter a string (less than 10 characters): \ n"); STR = string_create (); While (1) {printf ("*************************** \ n "); printf ("string operation exercise: \ n"); printf ("1. string Length \ n "); printf (" 2. string replication \ n "); printf (" 3. string connection \ n "); printf (" 4. substring \ n "); printf (" 5. comparison string \ n "); printf (" 6. String insertion \ n "); printf (" 7. string deletion \ n "); printf (" 8. string positioning \ n "); printf (" 9. string output \ n "); printf (" 10. exit program \ n "); printf (" input select: \ n "); scanf (" % d ", & choice); getchar (); Switch (choice) {Case 1: ANS = string_length (STR); printf ("the string length is % d \ n", ANS); break; Case 2: c = string_copy (C, str); printf ("target string: \ n"); string_show (c); break; Case 3: printf ("input string (less than 10 characters) \ n "); gets (c); C = string_connect (C, STR); printf (" the new string is \ n "); string_show (c); break; case 4: PRI Ntf ("Enter the position and length of the substring \ n"); scanf ("% d", & Pos, & Len); C = string_substr (C, STR, POs, Len); If (C = NULL) printf ("failed to request the substring! \ N "); else {printf (" the substring is \ n "); string_show (c) ;}break; Case 5: printf (" input string: \ n "); gets (c); ans = string_compare (C, STR); If (ANS> 0) printf (" the first string is large \ n "); else if (ANS <0) printf ("the second string is large \ n"); elseprintf ("the two strings are equal \ n"); break; Case 6: printf ("input string to be inserted in the main string \ n"); gets (c); printf ("input insert position: \ n "); scanf ("% d", & Pos); STR = string_insert (STR, C, POS); printf ("New String: \ n"); string_show (STR ); break; Case 7: printf ("Enter the start position and length of the substring \ n"); scanf ("% d % D ", & Pos, & Len); STR = string_delete (STR, POs, Len); break; case 8: printf (" Enter the substring to be located: \ n "); gets (c); ans = string_index (STR, C, 1); printf (" the result is % d \ n ", ANS); break; case 9: string_show (STR); break; case 10: Return 0; break; default: printf ("the selection is invalid! \ N "); break ;}} return 1 ;}// create a string of char * string_create () {char * s, ch; int I = 0; S = (char *) malloc (maxsize); CH = getchar (); While (Ch! = '#') {* (S + I) = CH; I ++; CH = getchar ();} if (I> maxsize/2) printf ("the input length is greater than 10! \ N "); else * (S + I) = '\ 0'; return s;} // returns the string length int string_length (char * s) {int L = 0; while (* s! = '\ 0') {L ++; s ++;} return l ;}// copy a string of char * string_copy (char * D, char * s) {char * C; C = D; while (* D ++ = * s ++ )! = '\ 0'); Return C;} // string connection char * string_connect (char * D, char * s) {char * C; int L, I = 0 ;; C = D; L = string_length (d); D = d + L; while (* D ++ = * s ++ )! = '\ 0'); // returns C;} // returns the substring char * string_substr (char * D, char * s, int POs, int Len) {char * C1, * C2 = NULL; int L, I; C2 = (char *) malloc (maxsize/2); C1 = s; D = c2; L = string_length (s); If (Pos> L | POS <1) // return NULL is invalid in the input position; if (LEN <0) // return NULL is invalid in the input length; c1 = C1 + pos-1; for (I = 1; I <= Len & * C1! = '\ 0'; I ++) // evaluate the string {* C2 ++ = * C1 ++;} * C2 =' \ 0 '; // do not forget the end symbol return D;} // string comparison int string_compare (char * D, char * s) {char * C1, * C2; C1 = D; c2 = s; while (* C1! = '\ 0' | * C2! = '\ 0') {If (* C1> * C2) return 1; else if (* C1 <* C2) Return-1; C1 ++; C2 ++ ;} if (* C1 = '\ 0' & * C2 =' \ 0') // when both ends, the two strings are equal and return 0; else if (* C2 = '\ 0') // The second string ends first, then the first string returns 1; elsereturn-1 ;} // insert string char * string_insert (char * D, char * s, int POS) {int I, LD, ls; char * C1, * C2; C1 = D; c2 = s; LD = string_length (d); LS = string_length (s); for (I = LD; I> = pos-1; I --) // string move back, leave the position of the string to be inserted * (C1 + LS + I) = * (C1 + I); for (I = Pos; I <= POS + ls-1; I ++) // Insert the sub-string * (C1 + I-1) = * C2 ++; * (C1 + LD + ls) = '\ 0 '; // The last ending symbol cannot forget return D;} // delete a char * string_delete (char * D, int POs, int Len) {int I, L; L = string_length (d); If (Pos + Len> L) // If the delete length is greater than the length after the start position of the string, then only keep the main string before the character * (D + pos-1) = '\ 0'; else {for (I = POS + len-1; I <= L; I ++) * (D + I-len) = * (D + I); * (D + L-len) = '\ 0'; // end character} return D ;} // locate the int string_index (char * D, char * s, int POS) of the substring {int I = pos-1, j = 0, LD, ls, B = 0; LD = string_length (d); LS = string_length (s); While (I <LD & J <ls) {If (* (D + I) = * (S + J) // if the current character is equal, {I ++; j ++ ;} else // next match {I = I-j + 1; j = 0 ;}} if (j = ls) // return (I-LS + 1); elsereturn 0;} // output string void string_show (char * s) {While (putchar (* s ++ )); printf ("\ n ");}

The above code is related to the string operation.

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.