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
How connect plc and pc through software
Why should a c++ programmer be interested in stl?
Is there any error below, its a code to delete all entires from a map #include
Who created stl?
write a program that will accept a number and print.its equivalent in words the maximum input number is 9999
When did c++ add stl?
i wanted to know about questions about c,c++ , which is required for placements.... im a fresher
Define stl.
sir please send me bpcl previous question papers
What are the various types of stl containers?
Is string part of stl?
What is meant by stl in c++?
write a program to convert a decimal number in to its equivalent binary number?
how to making game in c++ ?
How do I convert a stl file?