Details of data structure string operation examples

  • 2020-05-26 09:47:26
  • OfStack

Details of data structure string operation examples

A string is a special linear table with one character per node, so a string is also called a string.

About the operation of the string mainly have to find the string length, string copy, string connection, find the substring, string insert, string delete, substring positioning and so on. String operation is also a part of the C language written test.

The following code implements the main operations of the string.


#include <stdio.h> 
#include <stdlib.h> 
 
#define MAXSIZE 20 
char *String_Create();         // Create a list of  
int String_Length(char *s);       // O long string  
void String_Show(char *s);       // The output string  
char *String_Copy(char *d, char *s);  // String of replication  
char *String_Connect(char *d, char *s); // Series connection  
char *String_SubStr(char *d, char *s, int pos, int len);  // Strives for the substring  
int String_Compare(char *d, char *s);  // String comparison  
char *String_Insert(char *d, char *s, int pos);   // String of insert  
char *String_Delete(char *d, int pos, int len);   // String to delete  
int String_Index(char *d, char *s, int pos);    // String matching  
 
int main(void) 
{ 
  int choice; 
  char *str, *c; 
  int ans, pos, len; 
 
  c = (char*)malloc(sizeof(MAXSIZE)); 
  printf(" Please enter the 1 String of a string ( Less than 10 A character ):\n"); 
  str = String_Create(); 
   
  while(1) 
  { 
    printf("***************************\n"); 
    printf(" String manipulation exercise :\n"); 
    printf("1. O long string \n"); 
    printf("2. String of replication \n"); 
    printf("3. Series connection \n"); 
    printf("4. Strives for the substring \n"); 
    printf("5. Compare the string \n"); 
    printf("6. String of insert \n"); 
    printf("7. String to delete \n"); 
    printf("8. String of positioning \n"); 
    printf("9. String output \n"); 
    printf("10. Exit the program \n"); 
     
    printf(" The input selection :\n"); 
    scanf("%d", &choice); 
    getchar(); 
 
    switch(choice) 
    { 
    case 1: 
      ans = String_Length(str); 
      printf(" The length of the string is zero %d\n", ans); 
      break; 
    case 2: 
      c = String_Copy(c, str); 
      printf(" The target string is :\n"); 
      String_Show(c); 
      break; 
    case 3: 
      printf(" The input string ( Less than 10 A character )\n"); 
      gets(c); 
      c = String_Connect(c, str); 
      printf(" The new string is \n"); 
      String_Show(c); 
      break; 
    case 4: 
      printf(" Enter you want to find the location and length of the substring \n"); 
      scanf("%d %d", &pos, &len); 
      c = String_SubStr(c, str, pos, len); 
      if(c == NULL) 
        printf(" Substring failed! \n"); 
      else 
      { 
        printf(" I want the string to be zero :\n"); 
        String_Show(c); 
      } 
      break; 
    case 5: 
      printf(" The input string :\n"); 
      gets(c); 
      ans = String_Compare(c, str); 
      if(ans>0) 
        printf(" Big first string \n"); 
      else if(ans<0) 
        printf(" The first 2 Big string \n"); 
      else 
        printf(" Two strings are equal \n"); 
      break; 
    case 6: 
      printf(" Enter the string to insert in the main string \n"); 
      gets(c); 
      printf(" Enter the insertion position :\n"); 
      scanf("%d", &pos); 
      str = String_Insert(str, c, pos); 
      printf(" The new string is :\n"); 
      String_Show(str); 
      break; 
    case 7: 
      printf(" Enter the starting position and length of the delete substring \n"); 
      scanf("%d %d", &pos, &len); 
      str = String_Delete(str, pos, len); 
      break; 
    case 8: 
      printf(" Enter the substring to locate :\n"); 
      gets(c); 
      ans = String_Index(str, c, 1); 
      printf(" The result of positioning is %d\n", ans); 
      break; 
    case 9: 
      String_Show(str); 
      break; 
    case 10: 
      return 0; 
      break; 
    default: 
      printf(" Selection is invalid !\n"); 
      break; 
    } 
  } 
  return 1; 
} 
 
// Create a list 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(" Input length greater than 10!\n"); 
  else 
    *(s+i) = '\0'; 
  return s; 
} 
 
// O long string  
int String_Length(char *s) 
{ 
  int l=0; 
 
  while(*s != '\0') 
  { 
    l++; 
    s++; 
  } 
 
  return l; 
} 
 
// List of copy  
char *String_Copy(char *d, char *s) 
{ 
  char *c; 
 
  c = d; 
  while((*d++=*s++)!='\0'); 
 
  return c; 
} 
 
// Series 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');    // Series connection  
 
  return c; 
} 
 
// Strives for 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)      // Illegal input location  
    return NULL; 
  if(len<0)          // Input length illegal  
    return NULL; 
  c1 = c1 + pos - 1; 
   
  for(i=1; i<=len && *c1 != '\0'; i++) // O string  
  { 
    *c2++ = *c1++; 
  } 
  *c2 = '\0';         // Don't forget the sign-off  
   
  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 characters end, the two strings are equal  
    return 0; 
  else if(*c2 == '\0')        // The first 2 The first string ends, then the first 1 Big string  
    return 1; 
  else 
    return -1; 
} 
 
// Insert the 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--)   // The string moves back, leaving the string position to be inserted  
    *(c1 + ls + i) = *(c1 + i);  
  for(i=pos; i<=pos+ls-1; i++) // Insert substring  
    *(c1 + i - 1) = *c2++; 
  *(c1 + ld + ls) = '\0';     // The final sign-off must not be forgotten  
 
  return d; 
} 
 
// String to delete  
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 of the string, only the first character of the main string is retained  
    *(d + pos -1) = '\0'; 
  else 
  { 
    for(i=pos+len-1; i<=l; i++) 
      *(d + i - len) = *(d + i); 
    *(d + l - len) = '\0';   // The end of the character  
  } 
 
  return d; 
} 
 
// Substring positioning  
int String_Index(char *d, char *s, int pos) 
{ 
  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 characters are equal, the match continues  
    { 
      i++; 
      j++; 
    } 
    else          // Under the 1 Trip to match  
    { 
      i = i - j + 1; 
      j = 0; 
    } 
  } 
  if(j == ls)     // The match is successful  
    return (i - ls + 1); 
  else 
    return 0; 
} 
 
// The output string  
void String_Show(char *s) 
{ 
  while(putchar(*s++)); 
  printf("\n"); 
} 

The code above is the operation of the string.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: