如何在串中刪除特定長度的子串

來源:互聯網
上載者:User

標籤:des   style   http   color   io   os   ar   for   div   

昨天我們寫了如何在串中插入另外一個串,那麼今天我們來看看對應的操作:如何在串中刪除指定長度的子串。也就是,從串S1中刪除第pos個字元起長度為len的子串。

  •  中原娛樂城其實就是數組操作啦,把第pos個元素起的len個元素去掉即可。具體怎麼去掉呢?把S[pos+len]賦值給S[pos],把S[pos+len+1]賦值給S[pos+1]……以此類推就OK了。

函數設計如下:

view source print?
01 /*  初始條件: 串S存在,1≤pos≤StrLength(S)-len+1 */
02 /*  操作結果: 從串S中刪除第pos個字元起長度為len的子串 */
03 Status StrDelete(String S,int pos,int len)
04 {
05     int i;
06     if(pos < 1||pos>S[0]-len+1||len < 0)
07         return ERROR;
08     for(i=pos+len; i<=S[0]; i++)
09         S[i-len]=S[i];
10     S[0]-=len;
11     return OK;
12 }

測試執行代碼為:

view source print?
1 case 8:
2     printf("從串s1的第pos個字元起,刪除len個字元,請輸入pos: \n");
3     scanf("%d", &i);
4     printf("再輸入len: \n");
5     scanf("%d", &j);
6     StrDelete(s1,i,j);
7     StrPrint(s1);
8     break;

程式運行結果:

view source print?
01 1.StrAssign 產生串
02 2.StrLength 求串長
03 3.StrCompare 串比較
04 4.Concat 串聯接
05 5.SubString 求子串
06 6.Index 求子串位置
07 7.StrInsert 子串插入
08 8.StrDelete 子串刪除
09 0.退出
10 請選擇你的操作:
11 1
12 串s1為:nowamagic.net
13  
14 8
15 從串s1的第pos個字元起,刪除len個字元,請輸入pos:
16 2
17 再輸入len:
18 3
19 nmagic.net

完整的可執行程式在後面給出。

如何在串中刪除特定長度的子串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.