我的作业是读取一个文件,存储在目标单词(以#分隔)和替换单词(也以#分隔)和原始字符串(它们没有#)中。
此外,我无法假设最大 str 长度或最大单词数。
例如:
#uic#e# // uic = target string e = replacement string
juice // find substring "uic" and replace it with 'e' resulting in "jee"
quicken // qeken
quiche // qehe
uicuicuick // eeek
#pp## // pp = target string nothing = replacement string
apples //ales
copper // coer
bopped //boed
#t#tttttt# // t = target string tttttt = replacement string
tut tut // ttttttutttttt ttttttutttttt
tttttttttttttttttttttttttttttttttttttttttttttttttttt // last string =
//# of t's * 6
<小时/>
除了弄清楚如何使用目标字符串和替换字符串之外,我已经做了所有事情。可以用strstr
来实现吗?我知道它指向第一次出现,但是是否有可能使其指向字符串中的每次出现?请使用伪代码向我展示。谢谢!
最佳答案
使用 strstr() 查找第一次出现的地址,将其称为 address
,然后您可以在 address + strlen(target_string)
上再次使用 strstr() 来查找字符串其余部分中出现的情况。
关于c - 如何查找多个出现的位置并使用字符串进行替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867765/