替换字符串中的单词 [英] Replacing a word in a string

查看:71
本文介绍了替换字符串中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我喜欢C因为很有趣。所以,我为lcc-win32

标准库编写了这个函数:strrepl。


我认为有这么多C头周围,​​也许我们可以在集体头脑风暴会议上改善它。


让我们在这里讨论一些C,换一个: - )


规格:

-----

功能:strrepl


概要


#include< string.h>

char * strrepl(char * InputString,char * StringToFind,

char * Replacement ,char *输出);

描述


strrepl函数在InputString中替换所有出现的

StringToFind by Replacement,写将内容修改为

输出字符串。原始输入字符串未被修改。


如果Output参数为NULL,则strrepl将返回需要

的空间(包括终止零)替换。


如果Replacement为NULL且Output不为NULL,则所有出现的

StringToFind都将被删除。


返回


strrepl函数返回替换所需的长度,如果

,其Output参数为NULL。如果没有,它会返回完成

替换的数量。


代码:

-----

#include< string.h>

int strrepl(char * InputString,char * StringToFind,

char * StringToReplace,char * output)

{

char * offset = NULL,* CurrentPointer = NULL;

int insertlen;

int findlen = strlen(StringToFind );

int result = 0;


if(StringToReplace)

insertlen = strlen(StringToReplace);

else

insertlen = 0;

if(输出){

if(output!= InputString)

memmove(输出,InputString,strlen(InputString)+1);

InputString = output;

}

else

result = strlen(InputString)+1;


while(* InputString){

offset = strstr(!offset?InputString:CurrentPointer ,StringTo Find);

if(offset == NULL)

break;

CurrentPointer =(offset +(output?insert) len:findlen));

if(输出){

strcpy(offset,(offset + findlen));

memmove(offset + insertlen,

offset,strlen(offset)+ 1);

if(insertlen)

memcpy(offset,StringToReplace,insertlen);

结果++;

}

else {

结果 - = findlen;

结果+ =插入;

}

}

返回结果;

}


欢迎各种评论,关于代码,界面

设计,文档等等。


jacob

Hi guys!

I like C because is fun. So, I wrote this function for the lcc-win32
standard library: strrepl.

I thought that with so many "C heads" around, maybe we could improve it
in a collective brainstorming session.

Let''s discuss some C here, for a change :-)

Specs:
-----
Function: strrepl

Synopsis

#include <string.h>
char *strrepl(char *InputString, char *StringToFind,
char *Replacement, char *Output);
Description

The strrepl function replaces in InputString all occurrences of
StringToFind by Replacement, writing the modified contents into the
Output string. The original input string is not modified.

If the Output argument is NULL, strrepl will return the space that would
be needed (including the terminating zero) for the replacement.

If Replacement is NULL and Output is not NULL, all occurrences of
StringToFind will be erased.

Returns

The strrepl function returns the needed length for the replacements if
its Output argument is NULL. If not, it returns the number of
replacements done.

Code:
-----
#include <string.h>
int strrepl(char *InputString,char *StringToFind,
char *StringToReplace,char *output)
{
char *offset = NULL, *CurrentPointer = NULL;
int insertlen;
int findlen = strlen(StringToFind);
int result = 0;

if (StringToReplace)
insertlen = strlen(StringToReplace);
else
insertlen = 0;
if (output) {
if (output != InputString)
memmove(output,InputString,strlen(InputString)+1);
InputString = output;
}
else
result = strlen(InputString)+1;

while (*InputString) {
offset = strstr(!offset?InputString:CurrentPointer,StringTo Find);
if (offset == NULL)
break;
CurrentPointer = (offset + (output ? insertlen : findlen));
if (output) {
strcpy (offset, (offset + findlen));
memmove (offset + insertlen,
offset, strlen (offset) + 1);
if (insertlen)
memcpy (offset, StringToReplace, insertlen);
result++;
}
else {
result -= findlen;
result += insertlen;
}
}
return result;
}

All kinds of comments are welcome, regarding the code, the interface
design, the documentation, etc etc.

jacob

推荐答案




jacob navia写于12/06/05 15:41,:


jacob navia wrote On 12/06/05 15:41,:

概要

#include< string.h>
char * strrepl(char * InputString,char * StringToFind,
char * Replacement,char * Output) ;


代码:
-----
#include< string.h>
int strrepl(char * InputString,C har * StringToFind,
char * StringToReplace,char * output)

Synopsis

#include <string.h>
char *strrepl(char *InputString, char *StringToFind,
char *Replacement, char *Output);
[...]

Code:
-----
#include <string.h>
int strrepl(char *InputString,char *StringToFind,
char *StringToReplace,char *output)




如果声明

和定义出现在同一翻译单元中。

(无论如何,在C中。您喜欢的另一种语言可能会有不同的规则。)


-
Er ********* @ sun.com



A diagnostic is required if both the declaration
and the definition appear in the same translation unit.
(In C, anyhow. That other language you''re fond of may
have different rules.)

--
Er*********@sun.com


jacob navia写道:
jacob navia wrote:
嗨伙计们!

我喜欢C因为很有趣。所以,我为lcc-win32
标准库编写了这个函数:strrepl。

我认为有这么多C头的话。也许我们可以在集体头脑风暴会议上改进它。

让我们在这里讨论一些C,换一个:-)

规格:
-----
功能:strrepl

剧情简介

< string.h>
char * strrepl(char * InputString,char * StringToFind,
^^^^^^

ITYM:int或size_t char * Replacement,char * Output);


为了避免名字冲突,我宁愿调用函数

str_repl或strRepl或replaceSubstring。

描述

strrepl函数在InputString中替换所有出现的Replacement的StringToFind,将修改后的内容写入
输出字符串。原始输入字符串未被修改。


那你的原型是错的:你想要

size_t str_repl(const char * restrict InputString,

const char * StringToFind,

const char *替换,

char * restrict输出);

以避免这种情况。或者如果你想要

来表示原始字符串没有通过

输入字符串来修改那么取消限制。

如果输出参数是NULL,strrepl将返回替换所需的空间(包括终止零)。

如果Replacement为NULL且Output不为NULL,则所有出现的
StringToFind将被删除。

返回

如果
的Output参数为NULL,则strrepl函数返回所需的替换长度。如果没有,则返回已完成的替换次数。


我更喜欢像snprintf()中的大小参数所以你

可以给出存储输出点的大小。


< snip:代码>
欢迎各种评论,包括代码,界面设计,文档等等。
Hi guys!

I like C because is fun. So, I wrote this function for the lcc-win32
standard library: strrepl.

I thought that with so many "C heads" around, maybe we could improve it
in a collective brainstorming session.

Let''s discuss some C here, for a change :-)

Specs:
-----
Function: strrepl

Synopsis

#include <string.h>
char *strrepl(char *InputString, char *StringToFind, ^^^^^^
ITYM: int or size_t char *Replacement, char *Output);
In order to avoid name clashes, I''d rather call the function
str_repl or strRepl or replaceSubstring.
Description

The strrepl function replaces in InputString all occurrences of
StringToFind by Replacement, writing the modified contents into the
Output string. The original input string is not modified.
Then your prototype is wrong: You want
size_t str_repl(const char* restrict InputString,
const char* StringToFind,
const char* Replacement,
char* restrict Output);
to avoid this. Or do away with the restrict if you want
to express that the original string is not modified via
InputString.
If the Output argument is NULL, strrepl will return the space that would
be needed (including the terminating zero) for the replacement.

If Replacement is NULL and Output is not NULL, all occurrences of
StringToFind will be erased.

Returns

The strrepl function returns the needed length for the replacements if
its Output argument is NULL. If not, it returns the number of
replacements done.
I would rather like a size parameter as in snprintf() so you
can give the size of the storage output points to.
<snip: Code>
All kinds of comments are welcome, regarding the code, the interface
design, the documentation, etc etc.




- 我宁愿瞄准参数的标准订单,

说领先

目的地,尺寸,

后跟

1)replacestr,source,findstr



2)source,replacestr,findstr

1)的优点是所有可以有特殊值的b / b $ b值的参数都会被收集起来。


- 我宁愿返回size_t as strlen()返回size_t


- 我不会重载返回值语义依赖于目标上的
- 用户无法以这种方式检测错误。

我宁愿满足于返回

完全替换目的字符串并返回0

错误。

如果需要更换次数,请使用额外的

size_t *参数。


- 给str_n_repl变种允许限制

替换的数量。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



- I would rather aim for a standard order of the parameters,
say leading
destination, size,
followed by either
1) replacestr, source, findstr
or
2) source, replacestr, findstr

1) has the advantage that all parameters which can have special
values are gathered to the start.

- I would rather return size_t as strlen() returns size_t

- I would not "overload" the return value semantics depending
on destination -- the user cannot detect errors this way.
I''d rather settle for returning the number of bytes needed for
the "fully replaced" destination string and returning 0 on
error.
If the number of replacements is needed, use an additional
size_t* parameter.

- give a str_n_repl variant allowing to restrict the number of
replacements.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Eric Sosman < ER ********* @ sun.com>写道:
Eric Sosman <er*********@sun.com> writes:
jacob navia写于12/06/05 15:41,:
jacob navia wrote On 12/06/05 15:41,:

#include< string.h>
char * strrepl(char * InputString,char * StringToFind,
char * Replacement,char * Output);

[...]

int strrepl (char * InputString,char * StringToFind,
char * StringToReplace,char * output)

#include <string.h>
char *strrepl(char *InputString, char *StringToFind,
char *Replacement, char *Output);

[...]

int strrepl(char *InputString,char *StringToFind,
char *StringToReplace,char *output)



如果声明
和定义出现在相同的翻译单位。



A diagnostic is required if both the declaration
and the definition appear in the same translation unit.




什么?你有没有听说过功能原型?自1989年左右以来,他们已经风靡一时。在此之前,我相信允许在定义之前使用函数

声明。


这里真正的问题是不同的返回类型。

-

char a [] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},* p

= b,i = 24; for(; p + =!* p; * p / = 4)开关(0 [p]& 3)情况0:{return 0; for(p - ; i - ; i - )case +

2:{i ++; if(i )break; else default:continue; if(0)case 1:putchar(a [i& 15]); break;}}}



What? Have you never heard of a "function prototype"? They''ve
been all the rage since 1989 or so. Even before that, function
declarations before definitions were, I believe, allowed.

The real problem here is the differing return types.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}


这篇关于替换字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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