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

Answer Posted / ankitecian

#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 ?    27 Yes 41 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How does an stl file work?

656


What are the various types of stl containers?

735


what is template and type convertion

1999


What is stl in c++ with example?

634


What do stl stand for?

643






Who wrote stl?

682


How do I convert a stl file?

568


What are the symptoms of stl?

607


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

1978


Do you like to Submit Questions in Bulk under Same Category?? Then use our Bulk ListerDo you like to Submit Questions in Bulk under Same Category?? Then use our Bulk Lister

1664


help me i need a c++ program which takes sequesnce of characters and outputed sequence of their token taypes, work same compiler in lexical analysis phase

1885


Describe the My Computer and My Documents folders; identify the elements that are present in every Window.

1859


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

2328


What two types of containers does the stl provide?

573


How do you convert stl to steps?

635