自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

刪除特定的字符的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)

開(kāi)發(fā) 開(kāi)發(fā)工具 算法
今天講講刪除特定的字符的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)。

一、需求描述

輸入一個(gè)長(zhǎng)字符串和一個(gè)短字符串,編寫(xiě)程序從長(zhǎng)字符串中將在短字符串出現(xiàn)過(guò)的字符刪除掉。

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

二、算法設(shè)計(jì)

我們可以通過(guò)將長(zhǎng)字符串中的字符逐個(gè)與短字符串中的字符相比較來(lái)判斷是否應(yīng)該將某個(gè)字符從長(zhǎng)字符串中刪除掉。

即如果長(zhǎng)字符串為“1234abcd”,短字符串為“2a”,那么先將長(zhǎng)字符串中的***個(gè)字符“1”分別與短字符串中的“2”和“a”相比較,發(fā)現(xiàn)都不相等,于是將字符“1”加入到新的字符串中;接著將長(zhǎng)字符串中的第二個(gè)字符“2”分別與短字符串中的“2”和“a”相比較,發(fā)現(xiàn)有相等的,于是不將字符“2”加入到新的字符串中;如此循環(huán)執(zhí)行,直到長(zhǎng)字符串中的所有字符都比較完成。

代碼

三、特殊流程考慮

在編寫(xiě)程序的過(guò)程中,我們要對(duì)輸入的字符串的長(zhǎng)度及格式多做考慮,如:

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

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

四、程序代碼

  1. /********************************************************************** 
  2. * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱(chēng): RemoveChars.c 
  4. * 文件標(biāo)識(shí): 無(wú) 
  5. * 內(nèi)容摘要: 在長(zhǎng)字符串中刪除在短字符串中出現(xiàn)過(guò)的字符 
  6. * 其它說(shuō)明: 例如, 長(zhǎng)字符串為"My name", 短字符串為"na", 那么結(jié)果為"My me" 
  7. * 當(dāng)前版本: V1.0 
  8. * 作    者: Zhou Zhaoxiong 
  9. * 完成日期: 20160318 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12.  
  13. // 重新定義數(shù)據(jù)類(lèi)型 
  14. typedef signed   char  INT8; 
  15. typedef          int   INT32; 
  16. typedef unsigned int   UINT32; 
  17.  
  18. // 函數(shù)聲明 
  19. void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr); 
  20.  
  21.  
  22. /********************************************************************** 
  23. * 功能描述: 主函數(shù) 
  24. * 輸入?yún)?shù): 無(wú) 
  25. * 輸出參數(shù): 無(wú) 
  26. * 返 回 值: 0-執(zhí)行成功   其它-執(zhí)行失敗 
  27. * 其它說(shuō)明: 無(wú) 
  28. * 修改日期        版本號(hào)     修改人            修改內(nèi)容 
  29. * --------------------------------------------------------------------- 
  30. * 20160318        V1.0     Zhou Zhaoxiong        創(chuàng)建 
  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.     // 判斷兩個(gè)字符串中是否有中文字符 
  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.     // 判斷短字符串的長(zhǎng)度是否超過(guò)了長(zhǎng)字符串 
  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.     // 調(diào)用函數(shù)從長(zhǎng)字符中將在短字符串中存在的字符刪除掉 
  72.     RemoveCharsFromStr(szInputLongStr, szInputShortStr); 
  73.      
  74.     return 0; 
  75.  
  76.  
  77. /********************************************************************** 
  78. * 功能描述: 從長(zhǎng)字符中將在短字符串中存在的字符刪除掉 
  79. * 輸入?yún)?shù): pszInputLongStr-輸入的長(zhǎng)字符串 
  80.              pszInputShortStr-輸入的短字符串 
  81. * 輸出參數(shù): 無(wú) 
  82. * 返 回 值: 無(wú) 
  83. * 其它說(shuō)明: 無(wú) 
  84. * 修改日期        版本號(hào)        修改人          修改內(nèi)容 
  85. * --------------------------------------------------------------------- 
  86. * 20160318        V1.0     Zhou Zhaoxiong        創(chuàng)建 
  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); 

五、程序測(cè)試

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

1.輸入長(zhǎng)字符串為“1234abcd”,短字符串為“2a”時(shí),程序運(yùn)行情況如下:

  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.輸入長(zhǎng)字符串為“Happy dog!”,短字符串為“ao”時(shí),程序運(yùn)行情況如下:

  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.輸入長(zhǎng)字符串為“我們123”,短字符串為“345”時(shí),程序運(yùn)行情況如下:

  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.輸入長(zhǎng)字符串為“12345”,短字符串為“234567”時(shí),程序運(yùn)行情況如下:

  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.輸入長(zhǎng)字符串為“abcdsf”,短字符串為“af2”時(shí),程序運(yùn)行情況如下:

  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 

六、需求擴(kuò)展

基于本文中的需求和程序,我們可考慮對(duì)需求進(jìn)行以下擴(kuò)展:

1.如果短字符串中的某個(gè)字符在長(zhǎng)字符串中存在,那么在長(zhǎng)字符串的對(duì)應(yīng)位置用空格占位,而不是直接將該字符從長(zhǎng)字符串中刪除。

 

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

【本文是51CTO專(zhuān)欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】

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

責(zé)任編輯:趙寧寧 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2016-12-29 17:14:41

回文串算法代碼

2016-12-30 13:32:24

字符串算法代碼

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

負(fù)載均衡算法實(shí)現(xiàn)

2016-12-29 11:18:26

前綴后綴C代碼

2023-01-24 17:03:13

強(qiáng)化學(xué)習(xí)算法機(jī)器人人工智能

2015-03-25 11:42:52

Java刪除特定元素

2023-02-26 22:33:32

字符串排列算法

2009-08-10 18:00:30

C#數(shù)據(jù)庫(kù)備份及還原

2009-08-11 10:26:49

C#算法C#字符串反轉(zhuǎn)

2017-03-02 10:49:37

推薦算法原理實(shí)現(xiàn)

2009-09-02 17:24:44

C#關(guān)機(jī)代碼

2009-08-18 13:35:06

C#枚舉文件

2023-12-20 08:35:54

Dijkstra算法A*算法計(jì)算機(jī)圖形學(xué)

2011-04-11 17:08:16

階乘算法C++

2023-12-08 09:15:53

Java單表樹(shù)形結(jié)構(gòu)Tree
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)