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 23 6482 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);
"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
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.
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
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.
"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
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.
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
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
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
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 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.
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
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 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. 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.
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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
| |