Write a program in C++ to concatenate two strings into third
string using pointers

Answer Posted / pankaj kumawat , jaipur

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

char * StrCat(const char*, const char*);
int StrLen(const char *);

int main(int argc, char *argv[])
{
char *FinalString = NULL;

if(argc < 3)
{
printf("Usage: <%s> <String -1> <String -2>\n",argv
[0]);
return -1;
}

FinalString = StrCat(argv[1],argv[2]);
printf("The Final String is::: \n[%s]\n",FinalString);
if(FinalString != NULL)
{
free(FinalString);
FinalString = NULL;
}
return 0;
}
char *StrCat(const char *_input1, const char *_input2)
{
char *_output;
int _strLen, _cntr1, _cntr2;
_strLen = StrLen(_input1)+StrLen(_input2)+1;
_output = (char *)malloc(_strLen);
memset(_output,'\0',_strLen);
_cntr1 = 0;
_cntr2 = 0;
while(*(_input1 + _cntr1) != NULL)
{
*(_output + _cntr1) = *(_input1 + _cntr1);
_cntr1++;
}
while(*(_input2 + _cntr2) != NULL)
{
*(_output + _cntr1) = *(_input2 + _cntr2);
_cntr1++;
_cntr2++;
}
return _output;
}

int StrLen(const char *_input)
{
int _len = 0;
while( *(_input + _len) != NULL)
{
_len++;
}
return _len;
}

Is This Answer Correct ?    9 Yes 30 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

In what scenario does the Logical file and Physical file being used?

2428


how to making game in c++ ?

2290


Name the different types of stl containers.

790


Is stl open source?

719


What does stl stand for in basketball?

728






how to use C++?

2157


How is stl different from c++ standard library?

847


Why should a c++ programmer be interested in stl?

725


i wanted to know about questions about c,c++ , which is required for placements.... im a fresher

1796


totoo po ba ang manga aliens!

2423


how can u do connectivity in c++ language? plz send me connectivity code in c++ ?

1997


What is a list in c++ stl?

784


To modify an, existing worksheet. What steps are involved for: 1. Inserting and deleting rows and columns. 2. Printing cell formulas 3Jld displayed values 3. Using the page setup command

1867


What does stl mean in slang?

729


Describe how to safeguard a system through acquisition of an antivirus Program and systematic backup.

1737