473,503 Members | 4,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace and concatenate a string

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
23 6544
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
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
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

"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
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
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
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
"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1294
by: tapan t | last post by:
I've something of this sort: stremail="task@yahoo.com;task1@yahoo.com;task2@yahoo.com" what I need to do is. I need to replace and concatenate it should look like this cdomail.Address...
13
2342
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
3
8789
by: Tom | last post by:
Hi I want to retrieve database data and concatenate them to a string. Here is my code .ToString()); //runtime error her } while (dr.NextResult()) string array =...
6
8814
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
5
2794
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
6
5779
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
14
27153
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
13
21782
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.