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
Is stl part of c++ standard?
how to use C++?
How do I convert a stl file?
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 ....
What is stl language?
How is stl different from c++ standard library?
What are stl algorithms?
What does stl stand for in basketball?
What are the different types of stl containers?
What are the symptoms of stl?
Who wrote stl?
Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.
What are the various types of stl containers?
What two types of containers does the stl provide?
What are the components of stl?