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

Answers were Sorted based on User's Feedback



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

Answer / naman patidar

#include <iostream.h>
#include <string.h>

void main(){
char *str1 = "First String";
char *str2 = "Second String";
int len = strlen(str1)+strlen(str2)+1;
char *result = new char[len];

int i ;
for( i =0 ; i<strlen(str1); i++){
result[i] = str1[i];
}
for(int j=0; j<strlen(str2); j++, i++) {
result[i] = str2[j];
}
result[i] ='\0';
cout<<result;
}

Is This Answer Correct ?    88 Yes 58 No

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

Answer / irshad

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
cout<<"\n enter first string \n";
cin>>str1;
cout<<"\n enter second string \n";
cin>>str2;
cout<<"\n the concatenated string is \n"<<str1<<str2;
getch();
}

Is This Answer Correct ?    28 Yes 31 No

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

Answer / atomic13

// I will only post the 2 functions I've used and the main()
one.

int StringLength(const char * s){
int l = 0;
while (*s++) l++;
return l;
}

char *StrCat(const char * str1, const char *str2){

int len1 = StringLength(str1);
int len2 = StringLength(str2);
int totLen = len1 + len2 + 1;

char * str12 = (char *)malloc((totLen)*sizeof(char));
memset(str12, '\0', totLen);

for (int i = 0; i < len1; i++)
*(str12 + i) = *(str1 + i);
for (int i = 0; i < len2; i++)
*(str12 + i + len1) = *(str2 + i);

return str12;
}


int main(int argc, char *argv[]){
char * S1= "ABCDE";
char * S2= "FGHIJ";

char *S12 = StrCat(S1, S2);
cout << "S12= "<< S12 << endl; // ABCDEFGH

return 0;
}

Is This Answer Correct ?    2 Yes 5 No

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

Answer / 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

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

Answer / 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

More STL Interview Questions

write a piece of c++ code which allocate memory to the 50 object of type CObj

2 Answers  


What is Constructor

14 Answers   Angel Broking,


what is electronic software

1 Answers  


Name the different types of stl containers.

0 Answers  


#define CUBE(x) (x*x*x) main() { int a,b=3; a=cube(b++); printf("%d %d",a,b); } What should be the value of a and b? My calc a=4 but syst a=6 how pls tell me if you know it?

12 Answers   Wipro,






What are the components of stl?

0 Answers  


What is stl language?

0 Answers  


what is the difference between thread and process

1 Answers   Infosys,


what is a template?

2 Answers   Amazon, BITS, IBS, Wipro,


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.

0 Answers  


HOW TO GET "H1B" -VISA 4 USA FOR MY SON?HE HAD COMPLETED "MS"(IT)FROM AUSTALIA 2007.I AM WORKING IN U.S.A.

0 Answers  


What does stl stand for in basketball?

0 Answers  


Categories