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

Give the output of the following program main() {int ret; ret=fork();ret=fork();ret=fork();ret=fork(); if(!ret) printf("sun"); else printf("solaris");

8 Answers   Sun Microsystems,


what is the difference between thread and process

1 Answers   Infosys,


What is a standard template library (stl)?

0 Answers  


why does the execution of a c++ program start with main()???

11 Answers  


Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A.Explain how a pointer to function can be declared in C++? B.List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header (“Include”) Files.

0 Answers   GE, Infosys, Microsoft, NIM,






Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

0 Answers  


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

0 Answers   NDS,


give me the defination of inheritance?

5 Answers   Infosys,


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

0 Answers  


How stl is different from the c++ standard library?

0 Answers  


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

0 Answers   Ascent,


what is a template?

2 Answers   Amazon, BITS, IBS, Wipro,


Categories