成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

刪除特定的字符的算法設計及C代碼實現

開發 開發工具 算法
今天講講刪除特定的字符的算法設計及C代碼實現。

一、需求描述

輸入一個長字符串和一個短字符串,編寫程序從長字符串中將在短字符串出現過的字符刪除掉。

例如,長字符串為“1234abcd”,短字符串為“3a”,那么經程序處理之后的字符串為“124bcd”;又如,長字符串為“good bye”,短字符串為“obh”,那么經程序處理之后的字符串為“gd ye”。

二、算法設計

我們可以通過將長字符串中的字符逐個與短字符串中的字符相比較來判斷是否應該將某個字符從長字符串中刪除掉。

即如果長字符串為“1234abcd”,短字符串為“2a”,那么先將長字符串中的***個字符“1”分別與短字符串中的“2”和“a”相比較,發現都不相等,于是將字符“1”加入到新的字符串中;接著將長字符串中的第二個字符“2”分別與短字符串中的“2”和“a”相比較,發現有相等的,于是不將字符“2”加入到新的字符串中;如此循環執行,直到長字符串中的所有字符都比較完成。

代碼

三、特殊流程考慮

在編寫程序的過程中,我們要對輸入的字符串的長度及格式多做考慮,如:

1.如果輸入的兩個字符串之一含有中文字符,那么程序直接返回而不執行后續流程。

2.如果輸入的短字符串的長度大于了長字符串的長度,那么程序直接返回而不執行后續流程。

四、程序代碼

  1. /********************************************************************** 
  2. * 版權所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱: RemoveChars.c 
  4. * 文件標識: 無 
  5. * 內容摘要: 在長字符串中刪除在短字符串中出現過的字符 
  6. * 其它說明: 例如, 長字符串為"My name", 短字符串為"na", 那么結果為"My me" 
  7. * 當前版本: V1.0 
  8. * 作    者: Zhou Zhaoxiong 
  9. * 完成日期: 20160318 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12.  
  13. // 重新定義數據類型 
  14. typedef signed   char  INT8; 
  15. typedef          int   INT32; 
  16. typedef unsigned int   UINT32; 
  17.  
  18. // 函數聲明 
  19. void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr); 
  20.  
  21.  
  22. /********************************************************************** 
  23. * 功能描述: 主函數 
  24. * 輸入參數: 無 
  25. * 輸出參數: 無 
  26. * 返 回 值: 0-執行成功   其它-執行失敗 
  27. * 其它說明: 無 
  28. * 修改日期        版本號     修改人            修改內容 
  29. * --------------------------------------------------------------------- 
  30. * 20160318        V1.0     Zhou Zhaoxiong        創建 
  31. ***********************************************************************/ 
  32. INT32 main() 
  33.     INT8   szInputLongStr[100] = {0}; 
  34.     INT8   szInputShortStr[50] = {0}; 
  35.     UINT32 iPosFlag            = 0
  36.      
  37.     printf("Please input the long string: \n"); 
  38.     gets(szInputLongStr); 
  39.     printf("InputLongStr=%s\n", szInputLongStr); 
  40.  
  41.     printf("Please input the short string: \n"); 
  42.     gets(szInputShortStr); 
  43.     printf("InputShortStr=%s\n", szInputShortStr); 
  44.  
  45.     // 判斷兩個字符串中是否有中文字符 
  46.     for (iPosFlag = 0; iPosFlag < strlen(szInputLongStr); iPosFlag ++) 
  47.     { 
  48.         if (szInputLongStr[iPosFlag] < 0)     // 小于0則表示含有中文字符 
  49.         { 
  50.             printf("%s has Chinese character, please check!\n", szInputLongStr); 
  51.             return -1; 
  52.         } 
  53.     } 
  54.  
  55.     for (iPosFlag = 0; iPosFlag < strlen(szInputShortStr); iPosFlag ++) 
  56.     { 
  57.         if (szInputShortStr[iPosFlag] < 0)     // 小于0則表示含有中文字符 
  58.         { 
  59.             printf("%s has Chinese character, please check!\n", szInputShortStr); 
  60.             return -1; 
  61.         } 
  62.     } 
  63.  
  64.     // 判斷短字符串的長度是否超過了長字符串 
  65.     if (strlen(szInputShortStr) > strlen(szInputLongStr)) 
  66.     { 
  67.         printf("%s is longer than %s, please check!\n", szInputShortStr, szInputLongStr); 
  68.         return -2; 
  69.     } 
  70.  
  71.     // 調用函數從長字符中將在短字符串中存在的字符刪除掉 
  72.     RemoveCharsFromStr(szInputLongStr, szInputShortStr); 
  73.      
  74.     return 0; 
  75.  
  76.  
  77. /********************************************************************** 
  78. * 功能描述: 從長字符中將在短字符串中存在的字符刪除掉 
  79. * 輸入參數: pszInputLongStr-輸入的長字符串 
  80.              pszInputShortStr-輸入的短字符串 
  81. * 輸出參數: 無 
  82. * 返 回 值: 無 
  83. * 其它說明: 無 
  84. * 修改日期        版本號        修改人          修改內容 
  85. * --------------------------------------------------------------------- 
  86. * 20160318        V1.0     Zhou Zhaoxiong        創建 
  87. ***********************************************************************/ 
  88. void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr) 
  89.     INT8   szNewtStr[100] = {0}; 
  90.     UINT32 iOuterLoopFlag = 0
  91.     UINT32 iInnerLoopFlag = 0
  92.     UINT32 iCharUseFlag   = 0
  93.  
  94.     if (pszInputLongStr == NULL || pszInputShortStr == NULL) 
  95.     { 
  96.         return; 
  97.     } 
  98.  
  99.     memset(szNewtStr, 0x00, sizeof(szNewtStr)); 
  100.      
  101.     for (iOuterLoopFlag = 0; iOuterLoopFlag < strlen(pszInputLongStr); iOuterLoopFlag ++) 
  102.     { 
  103.         iCharUseFlag = 1
  104.         for (iInnerLoopFlag = 0; iInnerLoopFlag < strlen(pszInputShortStr); iInnerLoopFlag ++) 
  105.         { 
  106.             if (pszInputLongStr[iOuterLoopFlag] == pszInputShortStr[iInnerLoopFlag]) 
  107.             { 
  108.                 iCharUseFlag = 0;    // 不要將該字符加入新的字符串中 
  109.                 break; 
  110.             } 
  111.         } 
  112.      
  113.         if (iCharUseFlag == 1) 
  114.         { 
  115.             strncat(szNewtStr, pszInputLongStr+iOuterLoopFlag, 1); 
  116.         } 
  117.     } 
  118.      
  119.     printf("Remove chars of %s from %s, the new str is: %s\n", pszInputShortStr, pszInputLongStr, szNewtStr); 

五、程序測試

我們將編寫好的程序“RemoveChars.c”上傳到Linux機器,并使用“gcc -g -o RemoveCharsRemoveChars.c”命令對該程序進行編譯,生成“RemoveChars”文件。下面對程序進行詳細的測試。

1.輸入長字符串為“1234abcd”,短字符串為“2a”時,程序運行情況如下:

  1. Please input the long string: 
  2. 1234abcd 
  3. InputLongStr=1234abcd 
  4. Please input the short string: 
  5. 2a 
  6. InputShortStr=2a 
  7. Remove chars of 2a from 1234abcd, the new str is: 134bcd 

2.輸入長字符串為“Happy dog!”,短字符串為“ao”時,程序運行情況如下:

  1. Please input the long string: 
  2. Happy dog! 
  3. InputLongStr=Happy dog! 
  4. Please input the short string: 
  5. ao 
  6. InputShortStr=ao 
  7. Remove chars of ao from Happy dog!, the new str is: Hppy dg! 

3.輸入長字符串為“我們123”,短字符串為“345”時,程序運行情況如下:

  1. Please input the long string: 
  2. 我們123 
  3. InputLongStr=我們123 
  4. Please input the short string: 
  5. 345 
  6. InputShortStr=345 
  7. 我們123 has Chinese character, please check! 

4.輸入長字符串為“12345”,短字符串為“234567”時,程序運行情況如下:

  1. Please input the long string: 
  2. 12345 
  3. InputLongStr=12345 
  4. Please input the short string: 
  5. 234567 
  6. InputShortStr=234567 
  7. 234567 is longer than 12345, please check! 

5.輸入長字符串為“abcdsf”,短字符串為“af2”時,程序運行情況如下:

  1. Please input the long string: 
  2. abcdsf 
  3. InputLongStr=abcdsf 
  4. Please input the short string: 
  5. af2 
  6. InputShortStr=af2 
  7. Remove chars of af2 from abcdsf, the new str is: bcds 

六、需求擴展

基于本文中的需求和程序,我們可考慮對需求進行以下擴展:

1.如果短字符串中的某個字符在長字符串中存在,那么在長字符串的對應位置用空格占位,而不是直接將該字符從長字符串中刪除。

 

2.不限制輸入字符串中不能出現中文字符,即如果長字符串為“我們123”,短字符串為“我1”,那么經程序處理之后的字符串為“們23”。

【本文是51CTO專欄作者周兆熊的原創文章,作者微信公眾號:周氏邏輯(logiczhou)】

戳這里,看該作者更多好文

責任編輯:趙寧寧 來源: 51CTO專欄
相關推薦

2016-12-30 13:32:24

字符串算法代碼

2016-12-29 17:14:41

回文串算法代碼

2016-12-29 17:07:59

字符算法代碼

2016-12-30 13:16:51

字符串算法代碼

2016-12-30 13:37:50

字符串算法代碼

2016-12-29 15:58:00

字符串子串算法

2016-12-29 11:02:13

源目錄前綴算法

2018-07-27 08:39:44

負載均衡算法實現

2016-12-29 11:18:26

前綴后綴C代碼

2023-01-24 17:03:13

強化學習算法機器人人工智能

2015-03-25 11:42:52

Java刪除特定元素

2023-02-26 22:33:32

字符串排列算法

2009-08-10 18:00:30

C#數據庫備份及還原

2017-03-02 10:49:37

推薦算法原理實現

2009-08-11 10:26:49

C#算法C#字符串反轉

2009-08-18 13:35:06

C#枚舉文件

2009-09-02 17:24:44

C#關機代碼

2011-04-11 17:08:16

階乘算法C++

2023-12-20 08:35:54

Dijkstra算法A*算法計算機圖形學

2023-12-08 09:15:53

Java單表樹形結構Tree
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久99精品免费观看 | av黄在线观看 | 国产免费一区二区三区免费视频 | 欧美a级网站 | 操久久| 国产农村妇女精品一二区 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 国产精品久久久久一区二区三区 | 亚洲国产精品一区二区久久 | 日韩在线播放网址 | 国产探花在线精品一区二区 | 国产精品美女久久久免费 | 中文字幕一区二区三区精彩视频 | 99视频在线免费观看 | 亚洲天堂男人的天堂 | 亚洲久久一区 | 精产嫩模国品一二三区 | 99精品亚洲国产精品久久不卡 | 日本韩国欧美在线观看 | 狠狠狠干| 亚洲欧美中文日韩在线v日本 | 亚洲国产成人精品女人久久久 | 产真a观专区 | 蜜月aⅴ国产精品 | 久久伦理电影 | 免费在线看黄视频 | 日韩成人免费视频 | 成人a免费 | 精品欧美一区二区在线观看欧美熟 | 亚洲网站在线观看 | 国产一区二区久久 | 亚洲精品小视频在线观看 | 国产黄色在线观看 | 国产色爽| 日本成人二区 | www.久草.com | 日p视频免费看 | 久久久国产精品视频 | 亚洲精品字幕 | 日韩精品一区二区三区第95 | 中文字幕一区二区三区精彩视频 |