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
write a program to convert a decimal number in to its equivalent binary number?
how to use C++?
Who created stl?
Describe how to safeguard a system through acquisition of an antivirus Program and systematic backup.
Is there any error below, its a code to delete all entires from a map #include
draw a flowchart that accepts two numbers and checks if the first is divisible by the second.
Why should a c++ programmer be interested in stl?
How do I convert a stl file?
What are stl algorithms?
What is stl stack?
Is stl part of c++ standard?
Is stl open source?
What are the different types of stl containers?
Define stl.
What are the components of stl?