刪除特定的字符的算法設計及C代碼實現
一、需求描述
輸入一個長字符串和一個短字符串,編寫程序從長字符串中將在短字符串出現過的字符刪除掉。
例如,長字符串為“1234abcd”,短字符串為“3a”,那么經程序處理之后的字符串為“124bcd”;又如,長字符串為“good bye”,短字符串為“obh”,那么經程序處理之后的字符串為“gd ye”。
二、算法設計
我們可以通過將長字符串中的字符逐個與短字符串中的字符相比較來判斷是否應該將某個字符從長字符串中刪除掉。
即如果長字符串為“1234abcd”,短字符串為“2a”,那么先將長字符串中的***個字符“1”分別與短字符串中的“2”和“a”相比較,發現都不相等,于是將字符“1”加入到新的字符串中;接著將長字符串中的第二個字符“2”分別與短字符串中的“2”和“a”相比較,發現有相等的,于是不將字符“2”加入到新的字符串中;如此循環執行,直到長字符串中的所有字符都比較完成。
三、特殊流程考慮
在編寫程序的過程中,我們要對輸入的字符串的長度及格式多做考慮,如:
1.如果輸入的兩個字符串之一含有中文字符,那么程序直接返回而不執行后續流程。
2.如果輸入的短字符串的長度大于了長字符串的長度,那么程序直接返回而不執行后續流程。
四、程序代碼
- /**********************************************************************
- * 版權所有 (C)2016, Zhou Zhaoxiong。
- *
- * 文件名稱: RemoveChars.c
- * 文件標識: 無
- * 內容摘要: 在長字符串中刪除在短字符串中出現過的字符
- * 其它說明: 例如, 長字符串為"My name", 短字符串為"na", 那么結果為"My me"
- * 當前版本: V1.0
- * 作 者: Zhou Zhaoxiong
- * 完成日期: 20160318
- *
- **********************************************************************/
- #include <stdio.h>
- // 重新定義數據類型
- typedef signed char INT8;
- typedef int INT32;
- typedef unsigned int UINT32;
- // 函數聲明
- void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr);
- /**********************************************************************
- * 功能描述: 主函數
- * 輸入參數: 無
- * 輸出參數: 無
- * 返 回 值: 0-執行成功 其它-執行失敗
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ---------------------------------------------------------------------
- * 20160318 V1.0 Zhou Zhaoxiong 創建
- ***********************************************************************/
- INT32 main()
- {
- INT8 szInputLongStr[100] = {0};
- INT8 szInputShortStr[50] = {0};
- UINT32 iPosFlag = 0;
- printf("Please input the long string: \n");
- gets(szInputLongStr);
- printf("InputLongStr=%s\n", szInputLongStr);
- printf("Please input the short string: \n");
- gets(szInputShortStr);
- printf("InputShortStr=%s\n", szInputShortStr);
- // 判斷兩個字符串中是否有中文字符
- for (iPosFlag = 0; iPosFlag < strlen(szInputLongStr); iPosFlag ++)
- {
- if (szInputLongStr[iPosFlag] < 0) // 小于0則表示含有中文字符
- {
- printf("%s has Chinese character, please check!\n", szInputLongStr);
- return -1;
- }
- }
- for (iPosFlag = 0; iPosFlag < strlen(szInputShortStr); iPosFlag ++)
- {
- if (szInputShortStr[iPosFlag] < 0) // 小于0則表示含有中文字符
- {
- printf("%s has Chinese character, please check!\n", szInputShortStr);
- return -1;
- }
- }
- // 判斷短字符串的長度是否超過了長字符串
- if (strlen(szInputShortStr) > strlen(szInputLongStr))
- {
- printf("%s is longer than %s, please check!\n", szInputShortStr, szInputLongStr);
- return -2;
- }
- // 調用函數從長字符中將在短字符串中存在的字符刪除掉
- RemoveCharsFromStr(szInputLongStr, szInputShortStr);
- return 0;
- }
- /**********************************************************************
- * 功能描述: 從長字符中將在短字符串中存在的字符刪除掉
- * 輸入參數: pszInputLongStr-輸入的長字符串
- pszInputShortStr-輸入的短字符串
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ---------------------------------------------------------------------
- * 20160318 V1.0 Zhou Zhaoxiong 創建
- ***********************************************************************/
- void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr)
- {
- INT8 szNewtStr[100] = {0};
- UINT32 iOuterLoopFlag = 0;
- UINT32 iInnerLoopFlag = 0;
- UINT32 iCharUseFlag = 0;
- if (pszInputLongStr == NULL || pszInputShortStr == NULL)
- {
- return;
- }
- memset(szNewtStr, 0x00, sizeof(szNewtStr));
- for (iOuterLoopFlag = 0; iOuterLoopFlag < strlen(pszInputLongStr); iOuterLoopFlag ++)
- {
- iCharUseFlag = 1;
- for (iInnerLoopFlag = 0; iInnerLoopFlag < strlen(pszInputShortStr); iInnerLoopFlag ++)
- {
- if (pszInputLongStr[iOuterLoopFlag] == pszInputShortStr[iInnerLoopFlag])
- {
- iCharUseFlag = 0; // 不要將該字符加入新的字符串中
- break;
- }
- }
- if (iCharUseFlag == 1)
- {
- strncat(szNewtStr, pszInputLongStr+iOuterLoopFlag, 1);
- }
- }
- 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”時,程序運行情況如下:
- Please input the long string:
- 1234abcd
- InputLongStr=1234abcd
- Please input the short string:
- 2a
- InputShortStr=2a
- Remove chars of 2a from 1234abcd, the new str is: 134bcd
2.輸入長字符串為“Happy dog!”,短字符串為“ao”時,程序運行情況如下:
- Please input the long string:
- Happy dog!
- InputLongStr=Happy dog!
- Please input the short string:
- ao
- InputShortStr=ao
- Remove chars of ao from Happy dog!, the new str is: Hppy dg!
3.輸入長字符串為“我們123”,短字符串為“345”時,程序運行情況如下:
- Please input the long string:
- 我們123
- InputLongStr=我們123
- Please input the short string:
- 345
- InputShortStr=345
- 我們123 has Chinese character, please check!
4.輸入長字符串為“12345”,短字符串為“234567”時,程序運行情況如下:
- Please input the long string:
- 12345
- InputLongStr=12345
- Please input the short string:
- 234567
- InputShortStr=234567
- 234567 is longer than 12345, please check!
5.輸入長字符串為“abcdsf”,短字符串為“af2”時,程序運行情況如下:
- Please input the long string:
- abcdsf
- InputLongStr=abcdsf
- Please input the short string:
- af2
- InputShortStr=af2
- Remove chars of af2 from abcdsf, the new str is: bcds
六、需求擴展
基于本文中的需求和程序,我們可考慮對需求進行以下擴展:
1.如果短字符串中的某個字符在長字符串中存在,那么在長字符串的對應位置用空格占位,而不是直接將該字符從長字符串中刪除。
2.不限制輸入字符串中不能出現中文字符,即如果長字符串為“我們123”,短字符串為“我1”,那么經程序處理之后的字符串為“們23”。
【本文是51CTO專欄作者周兆熊的原創文章,作者微信公眾號:周氏邏輯(logiczhou)】