C/C++字元編輯

來源:互聯網
上載者:User
  1. //這兒有兩個函數,能進行在一個字串中的插入和刪除一個字元,
  2. //有興趣的可以參考一下.
  3. #include<string.h>
  4. #include<ctype.h>

  5. /*插入函數 ccode待插入的字元 anystring被插入的字串 spos插入到字串的位置*/
  6. void cinsert(char ccode,char *anystring,int spos)
  7. {
  8.     int p;  
  9.     p=strlen(anystring);  /*字串的長度*/
  10.     spos=spos<0?0:spos;  /*插入範圍*/
  11.     spos=spos>=p?p:spos;
  12.     for(;p>=spos;p--)
  13.         anystring[p+1]=anystring[p]; /*從數組最後那那個元素開始向上加*/
  14.     anystring[spos]=ccode;  /*插入該字元*/
  15. }

  16. /*刪除函數 anystring被刪除的字串 spos刪除第幾個字元*/
  17. void cdelete(char *anystring,int spos)
  18. {
  19.     int p;
  20.     p=strlen(anystring);  /*字串的長度*/
  21.     if(p>0&&spos>=0&&spos<=p){
  22.         while(spos<p)
  23.         {
  24.             anystring[spos]=anystring[spos+1];
  25.             spos++;
  26.         }
  27.     }
  28. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.