6-27 D字符串的刪除函數 (5分)


6-27 D字符串的刪除函數 (5分)
 

D字符串是動態分配內存的字符串,它也采用char數組來保存字符串中的字符,但是這個數組是在堆中動態分配得到的。

本題要求編寫D字符串的字符刪除函數。

函數接口定義:

char *dstr_remove(char *s, int index); 
 

dstr_remove刪除s在index位置上的字符,返回新的字符串。index從0開始。

裁判測試程序樣例:

#include <stdio.h> #include <stdlib.h> #include <string.h> // 這4個函數由系統提供 char *dstr_readword(); char *dstr_create(const char *s); char *dstr_add(char *s, char c); char *dstr_concat(char *this, const char *that); char *dstr_remove(char *s, int index); int main() { char *s = dstr_create("hello"); s = dstr_add(s, '!'); printf("%lu-%s\n", strlen(s), s); s = dstr_remove(s,0); printf("%lu-%s\n", strlen(s), s); char *t = dstr_readword(); s = dstr_remove(dstr_concat(s, t), strlen("hello")-1); free(t); printf("%lu-%s\n", strlen(s), s); free(s); } /* 請在這里填寫答案 */ 
 

輸入樣例:

123A
 

輸出樣例:

6-hello!
5-ello!
8-ello123A


char *dstr_remove(char *s, int index)
{
 int i;
 int str=strlen(s);
 for(i=index;i<str;i++)
 {
  s[i]=s[i+1];
 }
 return s;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM