【字符串处理算法】删除特定的字符的算法设计及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 
   
   
    
    

// 重新定义数据类型
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”。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知识的港湾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值