在给定字符串中查找字符串并将其替换为第3个字符串 [英] Find a string in a Given String and replace it with 3rd string

查看:80
本文介绍了在给定字符串中查找字符串并将其替换为第3个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数和原型是



char *替换(char * OriginalString,char * findString,char * ReplaceString);



i必须实现它



i必须在OriginalString中找到字符串FindString然后在原始字符串中找到该字符串然后我必须用Replacestring替换它。



如何使用字符串函数实现它而不使用字符串函数??



谢谢...

I m having a function and prototype is as

char* Replace(char *OriginalString,char *findString,char *ReplaceString);

i have to implement it as

i have to find the string FindString in OriginalString and then if that string found in the original string then i have to replace it with Replacestring.

How to implement it using string function and without using string functions??

Thanks...

推荐答案

要解决此问题,您需要编写代码,该代码使用至少一个额外的标记变量迭代OriginalString的字符,该变量指示字符开始匹配findString的点。当角色不匹配时,标记会被重置。当OriginalString或findString用完字符时,迭代结束,你点击你选择的结束标记,大概是NULL。然后您要么匹配,要么不匹配。

此时您必须选择使用ReplaceString进行就地替换或更多通常为替换为结果分配新字符串。按顺序写新字符串,匹配前的部分,替换部分,其余部分和你完成。

有许多C库函数可以帮助你,从 memcpy memmove strstr strncpy 如果您的C库提供它。

当你在首先匹配字符串或者处理你的字符串是UTF8编码或不是NULL终止时,这当然不会处理任何忽略大小写的问题。

如果生活中有任何这些伎俩,你的方式就会变得非常有趣和欢闹。记住你不是第一个想要这样做的人。有人必须在之前做过合理的工作。
To solve this you write code which iterates over the characters of the OriginalString with at least one extra marker variable which indicates the point at which the characters start to match findString. the marker is reset when characters don''t match. the iteration ends when OriginalString or findString run out of characters, you hit your chosen end marker, presumably NULL. You then either have a match or not.
At this point you have to choose to do an in place replacement with ReplaceString or more usually allocate a new string for the result with the replacement. Write the new string in order, the part before the match, the replacement part, the rest and you''re done.
There are a host of C library functions that can help you, from memcpy and memmove to strstr and strncpy if your C library provides it.
This of course doesn''t deal with any of the issues of ignoring case when you''re matching the strings in the first place or coping with your strings being UTF8 encoded or not being NULL terminated.
Much fun and hilarity your way cometh if life has pulled any of these tricks on you. Just remember you''re not the first person to want to do this. Somebody is bound to have done a reasonable job of it before.


在一般情况下你必须为新字符串分配内存,因此我们假设你的函数无论如何都会这样做。

你需要一个辅助功能,就是你必须推出自己的 strstr 版本。然后基于 findString OriginalString 内的出现次数以及 findString的长度 replaceString (这意味着你必须推出自己的 strlen 版本)可以计算新的字符串存储空间,例如

In the general case you have to allocate memory for the new string, hence let''s suppose your function will do it anyway.
You need an auxiliary function, that is you have to roll your own version of strstr. Then based on the number of occurrences of findString inside OriginalString and on the length of both findString and replaceString (that means you have to roll your own version of strlen too) you may compute the new string storage space, e.g.
int requiredStorage = originalLen + occurrences * (replaceLen-findLen) + 1;





一旦你分配了所需的空间,你可以开始实际构建结果字符串(粗略地):



Once you have allocated the required space you may start to actually build up the result string (roughly):

  1. originalString 复制到 resultString 直到发现 findstring
  2. originalString 为了跳过 findString 的出现
  3. replaceString 复制到 resultString
  4. 转到第1步
  1. copy from originalString into resultString until an occurrence of findstring is found
  2. advance pointer in originalString in order to skip the occurrence of findString
  3. copy replaceString to resultString
  4. go to step 1


这篇关于在给定字符串中查找字符串并将其替换为第3个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆