刪除特定的字符的算法設(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ù)流程。
四、程序代碼
- /**********************************************************************
- * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。
- *
- * 文件名稱(chēng): RemoveChars.c
- * 文件標(biāo)識(shí): 無(wú)
- * 內(nèi)容摘要: 在長(zhǎng)字符串中刪除在短字符串中出現(xiàn)過(guò)的字符
- * 其它說(shuō)明: 例如, 長(zhǎng)字符串為"My name", 短字符串為"na", 那么結(jié)果為"My me"
- * 當(dāng)前版本: V1.0
- * 作 者: Zhou Zhaoxiong
- * 完成日期: 20160318
- *
- **********************************************************************/
- #include <stdio.h>
- // 重新定義數(shù)據(jù)類(lèi)型
- typedef signed char INT8;
- typedef int INT32;
- typedef unsigned int UINT32;
- // 函數(shù)聲明
- void RemoveCharsFromStr(INT8 *pszInputLongStr, INT8 *pszInputShortStr);
- /**********************************************************************
- * 功能描述: 主函數(shù)
- * 輸入?yún)?shù): 無(wú)
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 0-執(zhí)行成功 其它-執(zhí)行失敗
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ---------------------------------------------------------------------
- * 20160318 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- 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);
- // 判斷兩個(gè)字符串中是否有中文字符
- 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;
- }
- }
- // 判斷短字符串的長(zhǎng)度是否超過(guò)了長(zhǎng)字符串
- if (strlen(szInputShortStr) > strlen(szInputLongStr))
- {
- printf("%s is longer than %s, please check!\n", szInputShortStr, szInputLongStr);
- return -2;
- }
- // 調(diào)用函數(shù)從長(zhǎng)字符中將在短字符串中存在的字符刪除掉
- RemoveCharsFromStr(szInputLongStr, szInputShortStr);
- return 0;
- }
- /**********************************************************************
- * 功能描述: 從長(zhǎng)字符中將在短字符串中存在的字符刪除掉
- * 輸入?yún)?shù): pszInputLongStr-輸入的長(zhǎng)字符串
- pszInputShortStr-輸入的短字符串
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ---------------------------------------------------------------------
- * 20160318 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- 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);
- }
五、程序測(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)行情況如下:
- 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.輸入長(zhǎng)字符串為“Happy dog!”,短字符串為“ao”時(shí),程序運(yùn)行情況如下:
- 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.輸入長(zhǎng)字符串為“我們123”,短字符串為“345”時(shí),程序運(yùn)行情況如下:
- Please input the long string:
- 我們123
- InputLongStr=我們123
- Please input the short string:
- 345
- InputShortStr=345
- 我們123 has Chinese character, please check!
4.輸入長(zhǎng)字符串為“12345”,短字符串為“234567”時(shí),程序運(yùn)行情況如下:
- Please input the long string:
- 12345
- InputLongStr=12345
- Please input the short string:
- 234567
- InputShortStr=234567
- 234567 is longer than 12345, please check!
5.輸入長(zhǎng)字符串為“abcdsf”,短字符串為“af2”時(shí),程序運(yùn)行情況如下:
- 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
六、需求擴(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)】