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

a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.

1383


write a program that will accept a number and print.its equivalent in words the maximum input number is 9999

2504


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

1785


What are the various types of stl containers?

735


Name the different types of stl containers.

695






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

2097


help me i need a c++ program which takes sequesnce of characters and outputed sequence of their token taypes, work same compiler in lexical analysis phase

1885


How do I convert a stl file?

568


What is stl language?

680


Define stl.

778


How does an stl file work?

656


What are stl algorithms?

627


What does stl mean in slang?

652


What is stl in c++ with example?

634


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 ....

1778