By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

replace and concatenate a string

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
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

Nov 15 '05 #2

P: n/a
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.
Nov 15 '05 #3

P: n/a
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);
Nov 15 '05 #4

P: n/a

"Jordan Abel" <jm****@purdue.edu> wrote in message
news:sl********************@random.yi.org...
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);


Neither of your 'solutions' does what OP asked for.

-Mike
Nov 15 '05 #5

P: n/a
On 2005-10-26, Mike Wahler <mk******@mkwahler.net> wrote:

"Jordan Abel" <jm****@purdue.edu> wrote in message
news:sl********************@random.yi.org...
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);


Neither of your 'solutions' does what OP asked for.


which is why i cancelled the post ten seconds later on rereading the
original.
Nov 15 '05 #6

P: n/a
Jordan Abel wrote
(in article <sl********************@random.yi.org>):
Neither of your 'solutions' does what OP asked for.


which is why i cancelled the post ten seconds later on rereading the
original.


When was the last time canceling worked? Over a decade ago?
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 15 '05 #7

P: n/a
Randy Howard <ra*********@FOOverizonBAR.net> writes:
Jordan Abel wrote
(in article <sl********************@random.yi.org>):
Neither of your 'solutions' does what OP asked for.


which is why i cancelled the post ten seconds later on rereading the
original.


When was the last time canceling worked? Over a decade ago?


For what it's worth, my newsreader showed me the header for the
cancelled article, but not the article itself. Apparently cancels
still work to some extent, but not reliably.

--
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.
Nov 15 '05 #8

P: n/a
"Peter Nilsson" <ai***@acay.com.au> wrote:
te****@gmail.com wrote:
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


And don't forget to allocate memory.

Richard
Nov 15 '05 #9

P: n/a
Keith Thompson <ks***@mib.org> wrote:
For what it's worth, my newsreader showed me the header for the
cancelled article, but not the article itself. Apparently cancels
still work to some extent, but not reliably.


FWIW, I actually can't see the cancelled article at all, and was
actually a little mystified by Mike's reply until I realized he had
replied to a cancelled post.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #10

P: n/a
Keith Thompson wrote
(in article <ln************@nuthaus.mib.org>):
Randy Howard <ra*********@FOOverizonBAR.net> writes:
Jordan Abel wrote
(in article <sl********************@random.yi.org>):
Neither of your 'solutions' does what OP asked for.

which is why i cancelled the post ten seconds later on rereading the
original.


When was the last time canceling worked? Over a decade ago?


For what it's worth, my newsreader showed me the header for the
cancelled article, but not the article itself. Apparently cancels
still work to some extent, but not reliably.


Sadly, you're totally dependent upon the software used by your
NNTP provider. Most seem to ignore cancels entirely these days.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 15 '05 #11

P: n/a
Christopher Benson-Manica wrote:
Keith Thompson <ks***@mib.org> wrote:
For what it's worth, my newsreader showed me the header for the
cancelled article, but not the article itself. Apparently cancels
still work to some extent, but not reliably.


FWIW, I actually can't see the cancelled article at all, and was
actually a little mystified by Mike's reply until I realized he had
replied to a cancelled post.


Same for me. So NIN (at least) honors cancels sometimes.


Brian

Nov 15 '05 #12

P: n/a
This is not for any homework. As far as defining the problem better
goes - I need to add "AB" right before ".txt". Assume the original
string is always some
filename.txt

Here is my implementation:

char* c="file.txt";
char* con="AB.txt";
int len=strlen(c);
len++;
char* newc=new char(len+2);//since I want to add "AB"
strcpy(newc,c);
strcpy(&newc[len-6],con);
newc[len+2]='\0';
Tj

Peter Nilsson wrote:
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


Nov 15 '05 #13

P: n/a
On Wed, 26 Oct 2005 10:08:02 -0700, tejasm wrote:
[quotes re-ordered]
Peter Nilsson wrote:
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.

This is not for any homework. As far as defining the problem better
goes - I need to add "AB" right before ".txt". Assume the original
string is always some
filename.txt


Are you guaranteed that both strings always end in ".txt"?
Are you guaranteed that the "AB" part will only ever be two characters?
Is it possible that the first string will ever be simply ".txt"?
Here is my implementation:

char* c="file.txt";
char* con="AB.txt";
int len=strlen(c);
len++;
char* newc=new char(len+2);//since I want to add "AB" ^^^^^^^^^

This is C++. A C solution would use malloc.
strcpy(newc,c);
strcpy(&newc[len-6],con);
At this point len is 9. len-6 is 3. newc[3] is 'e' whereas it seems you
want it to be '.'. You probably meant len-5. In any case, better style
is to use a macro or constant rather than a hard-coded value. I'd suggest
at the top of your code:

#define SUFFIX_LEN strlen("XX.txt")

and replacing [len-6] with [len+1-SUFFIX_LEN]
newc[len+2]='\0';


Other than that it looks OK. It's preferable to post compilable code (i.e
including required headers and a declaration of main).

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #14

P: n/a
te****@gmail.com writes:
This is not for any homework. As far as defining the problem better
goes - I need to add "AB" right before ".txt". Assume the original
string is always some
filename.txt

Here is my implementation:

char* c="file.txt";
char* con="AB.txt";
int len=strlen(c);
len++;
char* newc=new char(len+2);//since I want to add "AB"
strcpy(newc,c);
strcpy(&newc[len-6],con);
newc[len+2]='\0';


Please don't top-post. Your response goes below, or interspersed
with, any quoted text. See 99% of the followups in this newsgroup for
examples.

That's still not a precise statement of the problem.

It would probably make sense to write this as a function taking two
char* arguments and returning a char*. This raises the issue of
allocating and deallocating space for the result; there are several
approaches to that.

Assume *which* original string is "always some filename.txt"? There
are two of them.

What do you mean by "always some filename.txt"? Do you mean that the
string always ends in ".txt"?

You say you need to add "AB" right before ".txt". Presumably the "AB"
comes from the "con" variable, but how? Is it always going to be
"AB"?

And the code you posted is C++, not C.

--
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.
Nov 15 '05 #15

P: n/a
I can spend more time defining the problem, but obviously you are just
going to find more faults with my english rather than try and help me
out. Yes I agree, this is a C++ implementation, but I never said it was
C, it was just my implementation.
I wanted someone to correct my mistake (which Netocrat did, thanks!) or
give me suggestions about how else it could be implemented in C/C++.

I am not C/C++ guru like some of you, nor do I know the "rules" to this
newsgroup. I'll try my best in the future to define the problem better.
Tj

Nov 15 '05 #16

P: n/a
Thanks. Yes, both strings will always end with ".txt". Also, the string
to be inserted
will always have 2 characters before ".txt" i.e "AB". Also the first
string (Char* c) will never be only ".txt".

I'll post a compilable code next time.

Thanks again,
Tj

Nov 15 '05 #17

P: n/a
te****@gmail.com wrote:
I am not C/C++ guru like some of you, nor do I know the "rules" to this
newsgroup. I'll try my best in the future to define the problem better.


That's a situation that can be remedied.

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #18

P: n/a
te****@gmail.com wrote:

I am not C/C++ guru like some of you, nor do I know the "rules" to
this newsgroup. I'll try my best in the future to define the problem
better.

Also try to quote properly. See my .sig.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #19

P: n/a
te****@gmail.com writes:
I can spend more time defining the problem, but obviously you are just
going to find more faults with my english rather than try and help me
out. Yes I agree, this is a C++ implementation, but I never said it was
C, it was just my implementation.
I wanted someone to correct my mistake (which Netocrat did, thanks!) or
give me suggestions about how else it could be implemented in C/C++.

I am not C/C++ guru like some of you, nor do I know the "rules" to this
newsgroup. I'll try my best in the future to define the problem better.


Don't assume that your readers can easily see the article to which
you're replying. You need to provide some context.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And please complain to Google about their broken interface. Thank
you.

BTW, I *was* trying to help you out. It's not your English I was
complaining about, it was the fact that, as far as I can tell, you
never actually defined the problem you're trying to solve. You
provided an example, but I wasn't going to guess how it might
extrapolate to other examples.

In another followup in this thread, you wrote:

] Thanks. Yes, both strings will always end with ".txt". Also, the
] string to be inserted will always have 2 characters before ".txt"
] i.e "AB". Also the first string (Char* c) will never be only ".txt".

That's the kind of information I was looking for.

--
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.
Nov 15 '05 #20

P: n/a
Hi,

Proabably my solution is not a good solution but it can help and this
is what you want.

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

strncpy (result, c, strlen(c)-strlen(".txt"));
strcat (result, con);

puts (result);

PG.

Nov 15 '05 #21

P: n/a
Le 27-10-2005, Puneet <mr*************@gmail.com> a écrit*:
Hi,

Proabably my solution is not a good solution but it can help and this
is what you want.

char *c="file.txt";
char *con="AB.txt"
char *result;
And where is the variable pointed by result ?

This should produce better result:
char result[ sizeof("file.txt")-sizeof(".txt")+sizeof("AB.txt")];
strncpy (result, c, strlen(c)-strlen(".txt"));
Did you test it ? I am a bit tired, but I think that
the final '\0' is not copied.
strcat (result, con);
Why str*n*cpy in one case and strcat in the other ?
puts (result);


Marc Boyer
Nov 15 '05 #22

P: n/a
Agreed,

If you are using array not pointer then as marc suggest that is the
better way of declaring size.

Now another question of marc "Why str*n*cpy in one case and strcat in
the other ? "

answer is in first strncpy i am copying the string file in result
string. So only rquired part i am copying after turncating ".txt" from
that. And after that i concat the second string in result string for
required result.

PG.

Nov 15 '05 #23

P: n/a
On Wed, 26 Oct 2005 14:08:54 -0700, tejasm wrote:
Thanks. Yes, both strings will always end with ".txt". Also, the string
to be inserted
will always have 2 characters before ".txt" i.e "AB". Also the first
string (Char* c) will never be only ".txt".
Then your code looks OK, except that I noticed another off-by-one error in
the final line. I'll quote your original code with existing corrections
asterisked:
char* c="file.txt";
char* con="AB.txt";
int len=strlen(c);
len++;
char* newc=new char(len+2);//since I want to add "AB" strcpy(newc,c); * this is C++ - C would use malloc() strcpy(&newc[len-6],con); * off-by-one: should be [len-5] newc[len+2]='\0';

off-by-one: should be [len+1]

[...]
--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.