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 string part of stl?
What is a list in c++ stl?
Do you like to Submit Questions in Bulk under Same Category?? Then use our Bulk ListerDo you like to Submit Questions in Bulk under Same Category?? Then use our Bulk Lister
What are the components of stl?
sir please send me bpcl previous question papers
What are stl algorithms?
how to use C++?
how can u do connectivity in c++ language? plz send me connectivity code in c++ ?
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.
Is stl part of c++ standard?
What does stl mean in slang?
Explain stl.
What is stl stack?
What is stl stand for?
What is stl in oop?