replace and concatenate a string

T

tejasm

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
 
P

Peter Nilsson

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.
 
K

Keith Thompson

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.
 
J

Jordan Abel

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);
 
M

Mike Wahler

Jordan Abel said:
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
 
J

Jordan Abel

Jordan Abel said:
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.
 
R

Randy Howard

Jordan Abel wrote
(in article said:
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?
 
K

Keith Thompson

Randy Howard said:
Jordan Abel wrote


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.
 
C

Christopher Benson-Manica

Keith Thompson said:
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.
 
R

Randy Howard

Keith Thompson wrote
(in article said:
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.
 
D

Default User

Christopher said:
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
 
T

tejasm

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
 
N

Netocrat

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[/QUOTE]

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).
 
K

Keith Thompson

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.
 
T

tejasm

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
 
T

tejasm

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
 
D

Default User

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
 
K

Keith Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top