亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解

 更新時(shí)間:2017年07月07日 10:23:23   投稿:lqh  
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解

串是一種特殊的線性表,它的每個(gè)結(jié)點(diǎn)是一個(gè)字符,所以串也稱作字符串。

    關(guān)于串的操作主要有求串長(zhǎng),串復(fù)制,串連接,求子串,串插入,串刪除,子串定位等。串的操作也是C語言筆試中常考的一部分。

    下面的代碼實(shí)現(xiàn)了串的主要操作。

#include <stdio.h> 
#include <stdlib.h> 
 
#define MAXSIZE 20 
char *String_Create();         //創(chuàng)建串 
int String_Length(char *s);       //求串長(zhǎng) 
void String_Show(char *s);       //輸出串 
char *String_Copy(char *d, char *s);  //串復(fù)制 
char *String_Connect(char *d, char *s); //串連接 
char *String_SubStr(char *d, char *s, int pos, int len);  //求子串 
int String_Compare(char *d, char *s);  //串比較 
char *String_Insert(char *d, char *s, int pos);   //串插入 
char *String_Delete(char *d, int pos, int len);   //串刪除 
int String_Index(char *d, char *s, int pos);    //串匹配 
 
int main(void) 
{ 
  int choice; 
  char *str, *c; 
  int ans, pos, len; 
 
  c = (char*)malloc(sizeof(MAXSIZE)); 
  printf("請(qǐng)輸入一串字符串(小于10個(gè)字符):\n"); 
  str = String_Create(); 
   
  while(1) 
  { 
    printf("***************************\n"); 
    printf("字符串操作練習(xí):\n"); 
    printf("1.求串長(zhǎng)\n"); 
    printf("2.串復(fù)制\n"); 
    printf("3.串連接\n"); 
    printf("4.求子串\n"); 
    printf("5.比較串\n"); 
    printf("6.串插入\n"); 
    printf("7.串刪除\n"); 
    printf("8.串定位\n"); 
    printf("9.串輸出\n"); 
    printf("10.退出程序\n"); 
     
    printf("輸入選擇:\n"); 
    scanf("%d", &choice); 
    getchar(); 
 
    switch(choice) 
    { 
    case 1: 
      ans = String_Length(str); 
      printf("串的長(zhǎng)度為%d\n", ans); 
      break; 
    case 2: 
      c = String_Copy(c, str); 
      printf("目標(biāo)字符串為:\n"); 
      String_Show(c); 
      break; 
    case 3: 
      printf("輸入字串(小于10個(gè)字符)\n"); 
      gets(c); 
      c = String_Connect(c, str); 
      printf("新的字符串為\n"); 
      String_Show(c); 
      break; 
    case 4: 
      printf("輸入你想求子串所在位置及長(zhǎng)度\n"); 
      scanf("%d %d", &pos, &len); 
      c = String_SubStr(c, str, pos, len); 
      if(c == NULL) 
        printf("求子串失??!\n"); 
      else 
      { 
        printf("所求子串為:\n"); 
        String_Show(c); 
      } 
      break; 
    case 5: 
      printf("輸入字串:\n"); 
      gets(c); 
      ans = String_Compare(c, str); 
      if(ans>0) 
        printf("首字符串大\n"); 
      else if(ans<0) 
        printf("第二個(gè)字符串大\n"); 
      else 
        printf("兩字符串相等\n"); 
      break; 
    case 6: 
      printf("輸入需在主串中插入的字串\n"); 
      gets(c); 
      printf("輸入插入的位置:\n"); 
      scanf("%d", &pos); 
      str = String_Insert(str, c, pos); 
      printf("新的字符串為:\n"); 
      String_Show(str); 
      break; 
    case 7: 
      printf("輸入刪除子串的起始位置及長(zhǎng)度\n"); 
      scanf("%d %d", &pos, &len); 
      str = String_Delete(str, pos, len); 
      break; 
    case 8: 
      printf("輸入要定位的子串:\n"); 
      gets(c); 
      ans = String_Index(str, c, 1); 
      printf("定位的結(jié)果為%d\n", ans); 
      break; 
    case 9: 
      String_Show(str); 
      break; 
    case 10: 
      return 0; 
      break; 
    default: 
      printf("選擇無效!\n"); 
      break; 
    } 
  } 
  return 1; 
} 
 
//創(chuàng)建串 
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("輸入長(zhǎng)度大于10!\n"); 
  else 
    *(s+i) = '\0'; 
  return s; 
} 
 
//求串長(zhǎng) 
int String_Length(char *s) 
{ 
  int l=0; 
 
  while(*s != '\0') 
  { 
    l++; 
    s++; 
  } 
 
  return l; 
} 
 
//串的復(fù)制 
char *String_Copy(char *d, char *s) 
{ 
  char *c; 
 
  c = d; 
  while((*d++=*s++)!='\0'); 
 
  return c; 
} 
 
//串連接 
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');    //串連接 
 
  return c; 
} 
 
//求子串 
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; 
  if(len<0)          //輸入長(zhǎng)度非法 
    return NULL; 
  c1 = c1 + pos - 1; 
   
  for(i=1; i<=len && *c1 != '\0'; i++) //求字串 
  { 
    *c2++ = *c1++; 
  } 
  *c2 = '\0';         //不要忘記結(jié)尾符號(hào) 
   
  return d; 
} 
 
//串比較 
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')   //兩個(gè)字符處都結(jié)束時(shí),則兩字符串相等 
    return 0; 
  else if(*c2 == '\0')        //第二個(gè)字符串先結(jié)束,則第一個(gè)字符串大 
    return 1; 
  else 
    return -1; 
} 
 
//插入串 
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--)   //字符串后移,留出待插字符串位置 
    *(c1 + ls + i) = *(c1 + i);  
  for(i=pos; i<=pos+ls-1; i++) //插入子字符串 
    *(c1 + i - 1) = *c2++; 
  *(c1 + ld + ls) = '\0';     //最后的結(jié)尾符號(hào)不能忘掉 
 
  return d; 
} 
 
//串刪除 
char *String_Delete(char *d, int pos, int len) 
{ 
  int i, l; 
 
  l = String_Length(d); 
  if(pos + len > l)        //如果刪除長(zhǎng)度大于字串開始位置后面的長(zhǎng)度,則只保留主串前面字符 
    *(d + pos -1) = '\0'; 
  else 
  { 
    for(i=pos+len-1; i<=l; i++) 
      *(d + i - len) = *(d + i); 
    *(d + l - len) = '\0';   //結(jié)束字符 
  } 
 
  return d; 
} 
 
//子串定位 
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))  //當(dāng)前字符相等,則繼續(xù)匹配 
    { 
      i++; 
      j++; 
    } 
    else          //下一趟匹配 
    { 
      i = i - j + 1; 
      j = 0; 
    } 
  } 
  if(j == ls)     //匹配成功 
    return (i - ls + 1); 
  else 
    return 0; 
} 
 
//輸出串 
void String_Show(char *s) 
{ 
  while(putchar(*s++)); 
  printf("\n"); 
} 

上面的代碼就是串的相關(guān)操作。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • C++實(shí)現(xiàn)優(yōu)先隊(duì)列的示例詳解

    C++實(shí)現(xiàn)優(yōu)先隊(duì)列的示例詳解

    普通的隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),元素在隊(duì)列尾追加,而從隊(duì)列頭刪除。在優(yōu)先隊(duì)列中,元素被賦予優(yōu)先級(jí)。本文將用C++實(shí)現(xiàn)優(yōu)先隊(duì)列,需要的可以參考一下
    2022-06-06
  • C++實(shí)現(xiàn)五子棋游戲

    C++實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C語言實(shí)現(xiàn)電話訂餐管理系統(tǒng)

    C語言實(shí)現(xiàn)電話訂餐管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)電話訂餐管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言輸出旋轉(zhuǎn)后數(shù)組中的最小數(shù)元素的算法原理與實(shí)例

    C語言輸出旋轉(zhuǎn)后數(shù)組中的最小數(shù)元素的算法原理與實(shí)例

    這篇文章主要介紹了C語言輸出旋轉(zhuǎn)后數(shù)組中的最小數(shù)元素的算法原理與實(shí)例,數(shù)組旋轉(zhuǎn)就是把開頭的幾個(gè)指定的元素放到數(shù)組的末尾,需要的朋友可以參考下
    2016-03-03
  • C++設(shè)計(jì)模式之組合模式(Composite)

    C++設(shè)計(jì)模式之組合模式(Composite)

    這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之組合模式Composite,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • C語言實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)

    C語言實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++ std::any的模擬實(shí)現(xiàn)

    C++ std::any的模擬實(shí)現(xiàn)

    std::any是C++標(biāo)準(zhǔn)庫中的一個(gè)類,std::any對(duì)象可以存儲(chǔ)除單例等特殊情況外的任何類型的數(shù)據(jù),本文主要介紹了C++ std::any的模擬實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • C++ vector 遍歷的幾種方法

    C++ vector 遍歷的幾種方法

    本文主要介紹了C++ vector 遍歷的幾種方法,對(duì)vector 遍歷有一定的總結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-07-07
  • C++中auto類型說明符詳解(附易錯(cuò)實(shí)例)

    C++中auto類型說明符詳解(附易錯(cuò)實(shí)例)

    這篇文章主要給大家介紹了關(guān)于C++中auto類型說明符的相關(guān)資料,文中還附易錯(cuò)實(shí)例,在C++11中引入了auto類型說明符,用它就能讓編譯器替我們?nèi)シ治霰磉_(dá)式所屬的類型,需要的朋友可以參考下
    2023-07-07
  • C++?sqlite3數(shù)據(jù)庫配置使用教程

    C++?sqlite3數(shù)據(jù)庫配置使用教程

    SQLite 是一種嵌入式的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它是一個(gè)開源項(xiàng)目,已經(jīng)被廣泛應(yīng)用于各種應(yīng)用程序和操作系統(tǒng)中,這篇文章主要介紹了C++?sqlite3數(shù)據(jù)庫配置使用,需要的朋友可以參考下
    2023-08-08

最新評(píng)論