Click here to Skip to main content
15,881,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: concat two char * variables Pin
Rajesh R Subramanian20-Jan-10 6:26
professionalRajesh R Subramanian20-Jan-10 6:26 
GeneralRe: concat two char * variables Pin
santhosh-padamatinti20-Jan-10 6:33
santhosh-padamatinti20-Jan-10 6:33 
GeneralRe: concat two char * variables Pin
Fatbuddha 120-Jan-10 6:46
Fatbuddha 120-Jan-10 6:46 
QuestionRe: concat two char * variables Pin
CPallini20-Jan-10 10:47
mveCPallini20-Jan-10 10:47 
AnswerRe: concat two char * variables Pin
Luc Pattyn20-Jan-10 11:18
sitebuilderLuc Pattyn20-Jan-10 11:18 
GeneralRe: concat two char * variables Pin
CPallini20-Jan-10 11:23
mveCPallini20-Jan-10 11:23 
AnswerRe: concat two char * variables [modified] Pin
santhosh-padamatinti20-Jan-10 20:13
santhosh-padamatinti20-Jan-10 20:13 
GeneralRe: concat two char * variables Pin
CPallini20-Jan-10 21:20
mveCPallini20-Jan-10 21:20 
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/or strcpy:
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;
}


Smile | :)

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]

GeneralRe: concat two char * variables Pin
Saravanan Sundaresan21-Jan-10 18:52
professionalSaravanan Sundaresan21-Jan-10 18:52 
AnswerRe: concat two char * variables Pin
Avi Berger20-Jan-10 7:11
Avi Berger20-Jan-10 7:11 
AnswerRe: concat two char * variables Pin
David Crow20-Jan-10 7:53
David Crow20-Jan-10 7:53 
GeneralRe: concat two char * variables Pin
Richard MacCutchan20-Jan-10 11:50
mveRichard MacCutchan20-Jan-10 11:50 
GeneralRe: concat two char * variables Pin
Fatbuddha 120-Jan-10 20:13
Fatbuddha 120-Jan-10 20:13 
GeneralRe: concat two char * variables Pin
CPallini20-Jan-10 20:51
mveCPallini20-Jan-10 20:51 
GeneralRe: concat two char * variables Pin
Richard MacCutchan20-Jan-10 22:14
mveRichard MacCutchan20-Jan-10 22:14 
QuestionRe: concat two char * variables Pin
CPallini20-Jan-10 22:19
mveCPallini20-Jan-10 22:19 
AnswerRe: concat two char * variables Pin
Richard MacCutchan20-Jan-10 23:15
mveRichard MacCutchan20-Jan-10 23:15 
GeneralRe: concat two char * variables Pin
CPallini20-Jan-10 23:29
mveCPallini20-Jan-10 23:29 
GeneralRe: concat two char * variables Pin
Richard MacCutchan20-Jan-10 23:36
mveRichard MacCutchan20-Jan-10 23:36 
QuestionModal vs Modeless dialog box Pin
msr_codeproject20-Jan-10 6:05
msr_codeproject20-Jan-10 6:05 
AnswerRe: Modal vs Modeless dialog box Pin
Richard MacCutchan20-Jan-10 6:08
mveRichard MacCutchan20-Jan-10 6:08 
AnswerRe: Modal vs Modeless dialog box Pin
Rajesh R Subramanian20-Jan-10 6:58
professionalRajesh R Subramanian20-Jan-10 6:58 
QuestionRaeding and parsing mails via POP3 Pin
VictorSotnikov20-Jan-10 4:52
VictorSotnikov20-Jan-10 4:52 
AnswerRe: Raeding and parsing mails via POP3 Pin
Richard MacCutchan20-Jan-10 5:47
mveRichard MacCutchan20-Jan-10 5:47 
AnswerRe: Raeding and parsing mails via POP3 Pin
Joe Woodbury20-Jan-10 9:48
professionalJoe Woodbury20-Jan-10 9:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.