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

differentiate between private, public and protected data members of the class using example.

1 Answers  


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  


Write a program to print the swapping in two no and using three variable.

5 Answers   Broadridge,


What is the underlying datastructure of map?

5 Answers   BIA, Siemens,


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,






What is stl stack?

0 Answers  


What are the symptoms of stl?

0 Answers  


Which data structure gives efficient search? A. B-tree B. binary tree C. array D. linked list

21 Answers   ABC, Sun Microsystems,


WHAT IS THE DIFFERENCE BETWEEN C++ AND VC++

1 Answers   Syntel,


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

0 Answers   informatics,


how to swap two numbers in a linked list without exchanging the data but only the links?

3 Answers   Wipro,


sir please send me bpcl previous question papers

0 Answers   BPCL Bharat Petroleum,


Categories