替换并连接一个字符串 [英] replace and concatenate a string

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

问题描述




我有 -


char * c =" file.txt"

char * con =" AB.txt";


我想替换并连接上面的内容以获得新的字符串 -


" ; fileAB.txt"


提前致谢

TJ

解决方案

te****@gmail.com 写道:


我有 -

char * c =" file.txt"
char * con =" AB.txt" ;;

我想要替换和连接上面的内容以获得新的
字符串 -

" fileAB.txt"




提示:strrchr ,strncpy,strcpy


直到你出示证据证明你没有试图给b $ b b只是让其他人为你做功课,你就不是

可能会收到更多建议。


-

彼得


< blockq uote> te****@gmail.com 写道:

我有 -

char * c =" file.txt"
char * con =" AB.txt" ;;

我想替换并连接上面的内容以获取新的字符串 -

" fileAB.txt"




以下内容为您提供了所要求的内容,但几乎可以肯定

不是你想要的:


char * result =" fileAB.txt";


如果你想了解如何得到你想要的东西(大概是某些东西

,给出file.txt和AB.txt。会给你fileAB.txt),

你需要定义问题。你不能在C中实现它,如果你不能用b $ b来定义英语。


大概是''。''字符是这个问题很重要;因为就C而言,它只是另一个角色,你需要定义

究竟*它是多么重要。


要考虑的一些事情 :如果两个输入字符串都不是两个,那么如果
以.txt结尾怎么办?如果一个或另一个不包含''。''

字符怎么办?如果一个或另一个包含多个''。''

字符怎么办?如果一个或另一个开头或结尾有''。''

字符怎么办?


一旦你明确定义了问题,试试自己解决。如果

你被卡住了,随时可以发布你的代码,我们很乐意帮助你用它来支付



-

Keith Thompson(The_Other_Keith) ks***@mib.org < http: //www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


2005-10-26, te ** **@gmail.com < te **** @ gmail.com>写道:



我有 -

char * c =" file.txt"
char * con =" ; AB.txt" ;;

我想替换并连接上面的内容以获得新的字符串 -

" fileAB.txt"

提前致谢
TJ




char * x = malloc(strlen(c)+ strlen(con)+1);

memcpy(x,c,strlen(c));

memcpy(x + strlen(c),con,strlen(con)+1);


显然很奇怪的实现是允许编译器使用

strcpy和strcat可能比使用更简单的实现更好地优化



更简单[可能效率更低]的实现是,

当然,


char * x = malloc(strlen(c) + strlen(con)+1);

strcpy(x,c);

strcat(x,con);


Hi,

I have -

char* c="file.txt"
char* con="AB.txt";

I want to replace and concatenate the above to get a new string-

"fileAB.txt"

Thanks in advance
TJ

解决方案

te****@gmail.com wrote:

Hi,

I have -

char* c="file.txt"
char* con="AB.txt";

I want to replace and concatenate the above to get a new
string-

"fileAB.txt"



Hint: strrchr, strncpy, strcpy

Until you display evidence that you are not attempting to
simply have others do your homework for you, you are not
likely to receive greater advice.

--
Peter


te****@gmail.com writes:

I have -

char* c="file.txt"
char* con="AB.txt";

I want to replace and concatenate the above to get a new string-

"fileAB.txt"



The following gives you what you''re asking for, but almost certainly
not what you want:

char *result = "fileAB.txt";

If you want advice on how to get what you want (presumably something
that, given "file.txt" and "AB.txt" will give you "fileAB.txt"),
you''ll need to define the problem. You can''t implement it in C if you
can''t first define it in English.

Presumably the ''.'' character is significant to the problem; since it''s
just another character as far as C is concerned, you''ll need to define
exactly *how* it''s significant.

Some things to think about: What if the two input strings don''t both
end in ".txt"? What if one or the other doesn''t include a ''.''
character? What if one or the other contains more than one ''.''
character? What if one or the other starts or ends with a ''.''
character?

Once you''ve clearly defined the problem, try to solve it yourself. If
you get stuck, feel free to post your code and we''ll be glad to help
you with it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


On 2005-10-26, te****@gmail.com <te****@gmail.com> wrote:

Hi,

I have -

char* c="file.txt"
char* con="AB.txt";

I want to replace and concatenate the above to get a new string-

"fileAB.txt"

Thanks in advance
TJ



char *x = malloc(strlen(c)+strlen(con)+1);
memcpy(x,c,strlen(c));
memcpy(x+strlen(c),con,strlen(con)+1);

The apparently odd implementation is to allow the compiler to
potentially optimize better than a more naive implementation with a
strcpy and strcat could be.

The simpler [and possibly less efficient] implementation is, of
course,

char *x = malloc(strlen(c)+strlen(con)+1);
strcpy(x,c);
strcat(x,con);


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

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