实现字符串中子字符串的替换(待替换字符串和替换字符串的长度可以不相等)...

 

//使用C语言实现字符串中子字符串的替换
//描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),
//strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。
//举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,
//结果就变成了:ABCDEFGHIJKLMNOPQgggUVWXYZ

#include<stdio.h>
#include<string.h>

void StrReplace(char* strSrc, char* strFind, char* strReplace)
{
    int i,j,k,m;
    int lengthSrc,lengthFind,lengthReplace;
    lengthSrc = strlen(strSrc);
    lengthFind = strlen(strFind);
    lengthReplace = strlen(strReplace);

    for(i=0;i<lengthSrc;)
    {
        j = 0;
        if(strSrc[i] == strFind[j])//遍历原字符串,如果当前字符与strFind[0]相等,开始逐位判断
        {
            do
            {
                i++;j++;
            }while((j < lengthFind) && (strSrc[i] == strFind[j]));

            //判断跳出while循环的条件。如果j == lengthFind表示找到了匹配的字符串。且i指向strSrc中匹配字符串strFind的下一个字符。
            if(j == lengthFind)
            {
                //strFind字符串和strReplace字符串的长度相等时
                if(lengthFind == lengthReplace)
                {
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }
                }

                //strFind字符串的长度小于strReplace字符串的长度
                if(lengthFind < lengthReplace)
                {
                    //增加空位。
                    for(k=lengthSrc;k>=i;k--)
                    {
                        strSrc[k+lengthReplace-lengthFind] = strSrc[k];
                    }

                    //strSrc长度更新。如果不更新,在下一次增加空位时,会有问题。
                    lengthSrc += lengthReplace-lengthFind;

                    //开始替换。
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }

                    //strSrc的元素和长度变更后,需要将i重新指向已替换部分的下一个字符。
                    i+= lengthFind-lengthReplace;
                }
                
                //strFind字符串的长度大于strReplace字符串的长度
                if(lengthFind > lengthReplace)
                {
                    //减小空位。
                    for(k=i;k<=lengthSrc;k++)
                    {
                        strSrc[k-(lengthFind-lengthReplace)] = strSrc[k];
                    }

                    //strSrc长度更新。如果不更新,在下一次减小空位时,会有问题。
                    lengthSrc -= lengthFind-lengthReplace;

                    //开始替换。
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }

                    //strSrc的元素和长度变更后,需要将i重新指向已替换部分的下一个字符。
                    i-= lengthFind-lengthReplace;
                }
            }
        }
        else
        {
            i++;
        }
    }
}

int main()
{
    char strSrc[255],strFind[255],strReplace[255];
    gets(strSrc);
    gets(strFind);
    gets(strReplace);
    
    StrReplace(strSrc,strFind,strReplace);
    puts(strSrc);
    return 0;
}

转载于:https://www.cnblogs.com/Camilo/p/3837806.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用函数编程的reduce函数来实现计算字符串中子串出现的次数。 具体实现步骤如下: 1. 定义一个函数count_substring,该函数接受两个参数,分别为字符串s和子串sub。 2. 在count_substring函数,使用reduce函数对字符串s进行遍历,统计子串sub出现的次数。 3. 在reduce函数,定义一个累加器accumulator和一个当前值current_value。accumulator的初始值为。 4. 对于每个字符,如果当前值current_value与子串sub相等,则累加器accumulator加1。 5. 最后返回累加器accumulator的值,即为子串sub在字符串s出现的次数。 下面是具体的代码实现: ``` def count_substring(s, sub): return reduce(lambda acc, x: acc + (1 if s[x:x+len(sub)] == sub else ), range(len(s)-len(sub)+1), ) ``` 使用示例: ``` s = "hello world" sub = "l" print(count_substring(s, sub)) # 输出 3 ``` 上述代码,reduce函数的第一个参数是一个lambda表达式,该表达式接受两个参数,分别为累加器accumulator和当前值current_value。lambda表达式的返回值为累加器accumulator加上当前值current_value是否等于子串sub的结果。 reduce函数的第二个参数是一个range对象,用于遍历字符串s所有可能的子串。 reduce函数的第三个参数是累加器accumulator的初始值,即。 最终返回的结果即为子串sub在字符串s出现的次数。 ### 回答2: 字符串中子串出现的次数是一个常见的问题,可以用函数编程实现。要求编写一个函数,输入一个字符串和一个子串,输出子串在字符串出现的次数。 函数实现: 首先,定义一个函数,该函数接受两个参数,一个是字符串,一个是子串,返回子串在字符串出现的次数。具体实现可以参考以下代码: ``` def count_substring(str, substr): count = 0 start = 0 while True: index = str.find(substr, start) if index == -1: break count += 1 start = index + 1 return count ``` 在这个函数,我们使用了字符串的 find 方法来查找子串的位置。如果找到了子串,就增加计数器的值,然后从下一个位置开始查找,直到找不到为止。 在测试这个函数的时候,我们可以编写以下代码: ``` str = "hello world, world, world!" substr = "world" count = count_substring(str, substr) print(count) ``` 这段代码输出的结果应该是 3,因为子串 "world" 在字符串 "hello world, world, world!" 出现了 3 次。 这个函数实现简单,但是需要注意的是,它并不是计算子串出现的个数的最优解法。如果字符串非常大,子串出现的位置又非常多,该函数的执行效率可能会比较低。实际上,计算子串出现次数的最优解法是使用 KMP 算法,它的时间复杂度为 O(n),可以实现更高效的计算方法。 ### 回答3: 函数式编程是一种以函数为核心的编程模式,它强调将函数看作是一等公民,可以作为变量、参数和返回值等来使用。本题需要用函数式编程实现计算字符串中子串出现的次数,我们可以采用函数式编程的map、reduce、filter等方法来解决问题。 首先,我们需要编写一个函数来查找目标子串在字符串的位置。可以使用JavaScript的indexOf()函数。代码如下: ``` function findSubstring(str, subStr){ let count = 0; let index = 0; while ((index = str.indexOf(subStr, index)) !== -1) { count++; index += subStr.length; } return count; } ``` 接着,我们可以将这个函数传递到reduce()函数,使用reduce()函数对字符串数组的每个元素调用findSubstring()函数并求和得到最终结果。代码如下: ``` function countSubstring(arr, subStr) { return arr.reduce((acc, cur) => acc + findSubstring(cur, subStr), 0); } ``` 最后,我们需要将字符串转换成一个数组,以便于使用countSubstring()函数进行计算。可以使用split()函数将字符串按照给定的分隔符拆分成一个数组。例如下面的代码: ``` const str = 'Hello world, hello world!'; const arr = str.split(' '); const subStr = 'world'; console.log(countSubstring(arr, subStr)); // 输出为2 ``` 至此,我们就用函数式编程实现了计算字符串中子串出现次数的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值