Write a program in C++ to concatenate two strings into third
string using pointers
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
What is C++ could you enplane me please?
What is stl language?
Write a program in C/C++ to implement reader- writer problem
Explain stl.
What is a standard template library (stl)? What are the various types of stl containers?
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
Give two integer arrays A & B.A has n elements and B has ' n-1 ' elements . A has all the elements that are there in B. But B has one missing element. Write a function that takes arrays , A & B as imnput and finds the missing element in most optised manner .
What is Template Specialization?
Find the error in the following program struct point {struct point *next; int data; } x; main() {int...data; } x; main() {int i; for(x=p;x!=0;) x=x->next,x++; freelist(x); } freelist(x) {free(x); return }
how to overload << and >> operator in c++
What is the disadvantage of templates ?
What is the STL?