concat two char * variables
-
Rajesh R Subramanian wrote:
You could use strcat()[^] to concatenate strings.
Actually my requirement is "not use the strcat() function".
To invent something, you need a mountain of junk in your mind. ---------------------Thomas alva edison
sampath-padamatinti wrote:
Actually my requirement is "not use the strcat() function".
Why? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
sampath-padamatinti wrote:
Actually my requirement is "not use the strcat() function".
Why? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Are you new around here? It probably is one of these: - I let others do my homework - I am troll - strcat() isn't safe enough; I prefer strcat_s() :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Are you new around here? It probably is one of these: - I let others do my homework - I am troll - strcat() isn't safe enough; I prefer strcat_s() :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
Which one, sir? I suppose:
strcat
isn't safe enough, I prefer mess up with pointers myself. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
sampath-padamatinti wrote:
return c3
Are you really wanting to return a variable that will go out of scope when
conc()
ends?sampath-padamatinti wrote:
Please tell me how to concat(add) two char*.
Something like:
char *c3 = c1;
while (*c3)
c3++;while (*c3++ = *c2++)
;While not exact, this is roughly what
strcat()
does."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
sampath-padamatinti wrote:
Actually my requirement is "not use the strcat() function".
Why? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]My interviewer thrown a question like: He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions). required function prototype is
char\* concat(const char\* c1, const char\* c2)
I am unable to solve this one. Please give me the approach Thanx in advance.....
To invent something, you need a mountain of junk in your mind. ---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
-
DavidCrow wrote:
Something like: char *c3 = c1; while (*c3) c3++; while (*c3++ = *c2++) ;
:wtf:
MVP 2010 - are they mad?
Yes, I was wondering too
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
-
DavidCrow wrote:
Something like: char *c3 = c1; while (*c3) c3++; while (*c3++ = *c2++) ;
:wtf:
MVP 2010 - are they mad?
Why wondering? It is concise and elegant, functionally equivalent to
strcat
, here the complete functionchar * concat( char * c1, const char * c2)
{
char *c3 = c1;while (*c3)
c3++;while (*c3++ = *c2++)
;
return c1;
}here a test program:
#include <stdio.h>
void main()
{
char buf[100];
sprintf(buf, "hello");
char * str = " folks!";
printf("%s\n", concat(buf, str));
}He just missed the
const
reuqirement for the first argument. But this is an OP fault (i.e. David's prototype is better). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
My interviewer thrown a question like: He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions). required function prototype is
char\* concat(const char\* c1, const char\* c2)
I am unable to solve this one. Please give me the approach Thanx in advance.....
To invent something, you need a mountain of junk in your mind. ---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
sampath-padamatinti wrote:
I am unable to solve this one. Please give me the approach
David did, here. However, if you're stuck with the
const
requirement on the first argument, then:char * concat(const char * c1, const char * c2)
{
size_t size[2] = {strlen(c1), strlen(c2)};
char * c = new char[size[0]+size[1]+1];
strcpy(c, c1);
strcpy(c+size[0],c2);
return c;
}if you can't use
strlen
and/orstrcpy
:char * concat(const char * c1, const char * c2)
{
size_t len=0;
char * c;
const char *p;
p = c1;
while (*p++) len++;
p = c2;
while (*p++) len++;
c = new char[len+1];
p=c1;
while (*c=*p++) c++;
p=c2;
while (*c++=*p++) ;
return (c-len-1);
}Test program:
void main()
{
const char * str1 = "hello, ";
const char * str2 = "folks!";
char * result = concat(str1, str2);
printf("%s\n", result);
delete [] result;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Why wondering? It is concise and elegant, functionally equivalent to
strcat
, here the complete functionchar * concat( char * c1, const char * c2)
{
char *c3 = c1;while (*c3)
c3++;while (*c3++ = *c2++)
;
return c1;
}here a test program:
#include <stdio.h>
void main()
{
char buf[100];
sprintf(buf, "hello");
char * str = " folks!";
printf("%s\n", concat(buf, str));
}He just missed the
const
reuqirement for the first argument. But this is an OP fault (i.e. David's prototype is better). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What's that funny, Rick? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What's that funny, Rick? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]That code is too dangerous for words. As long as the length of
buf
is greater than the combined length ofc1
andc2
plus a null terminator it will work. But as soon as the result overflows all hell breaks loose. This maybe OK for a skilled developer such as yourself, but I would not suggest it as a solution for a newbie.MVP 2010 - are they mad?
-
That code is too dangerous for words. As long as the length of
buf
is greater than the combined length ofc1
andc2
plus a null terminator it will work. But as soon as the result overflows all hell breaks loose. This maybe OK for a skilled developer such as yourself, but I would not suggest it as a solution for a newbie.MVP 2010 - are they mad?
The code provide what provides
strcat
, no less no more. I knowstrcat
is a very dangerous function... :rolleyes: God once said: "The newbies should learnC
pointers or go to Hell managed". :-D Moreover, since that was an interview question (see [^]), the OP was expected to have such a skill. :) BTW My5
for the 'skilled developer'. :laugh:If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The code provide what provides
strcat
, no less no more. I knowstrcat
is a very dangerous function... :rolleyes: God once said: "The newbies should learnC
pointers or go to Hell managed". :-D Moreover, since that was an interview question (see [^]), the OP was expected to have such a skill. :) BTW My5
for the 'skilled developer'. :laugh:If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
My interviewer thrown a question like: He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions). required function prototype is
char\* concat(const char\* c1, const char\* c2)
I am unable to solve this one. Please give me the approach Thanx in advance.....
To invent something, you need a mountain of junk in your mind. ---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
Here is the function as per your requirement:
char* concat(const char* c1, const char* c2)
{
int size1 = 0, size2 = 0;for (int i = 0; c1\[i\] != '\\0'; i++) size1++; for (int j = 0; c2\[j\] != '\\0'; j++) size2++; char\* result = new char\[size1+size2\]; for(int i=0; i<size1; i++) { result\[i\] = c1\[i\]; } for(int j = 0; j <= size2; j++) { result\[size1+j\] = c2\[j\]; } result\[size1+size2\] = 0; return result;
}