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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is stl part of c++ standard?

816


how to use C++?

2250


How do I convert a stl file?

766


If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2. please answer my question ....

2024


What is stl language?

850


How is stl different from c++ standard library?

954


What are stl algorithms?

854


What does stl stand for in basketball?

835


What are the different types of stl containers?

861


What are the symptoms of stl?

815


Who wrote stl?

883


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

2351


What are the various types of stl containers?

921


What two types of containers does the stl provide?

749


What are the components of stl?

850