删除特定的字符的算法设计及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代码

2015-03-25 11:42:52

Java删除特定元素

2023-01-24 17:03:13

强化学习算法机器人人工智能

2023-02-26 22:33:32

字符串排列算法

2009-08-10 18:00:30

C#数据库备份及还原

2009-08-11 10:26:49

C#算法C#字符串反转

2017-03-02 10:49:37

推荐算法原理实现

2023-12-20 08:35:54

Dijkstra算法A*算法计算机图形学

2009-09-02 17:24:44

C#关机代码

2009-08-18 13:35:06

C#枚举文件

2011-04-11 17:08:16

阶乘算法C++

2023-12-08 09:15:53

Java单表树形结构Tree
点赞
收藏

51CTO技术栈公众号