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
Who created stl?
What are stl algorithms?
What is a list in c++ stl?
What is a standard template library (stl)?
What is stl stack?
write a program that will accept a number and print.its equivalent in words the maximum input number is 9999
sir please send me bpcl previous question papers
What are the components of stl?
Can we use stl in coding interviews?
Is string part of stl?
how to making game in c++ ?
How connect plc and pc through software
What are the symptoms of stl?
What is stl in c++ with example?
How do you convert stl to steps?