
Problem
Solution
Submissions
Concatenate Two Strings
Certification: Basic Level
Accuracy: 73.33%
Submissions: 15
Points: 10
Write a C++ program that concatenates two strings using pointers. The program should take two strings as input and return a new string that concatenates the two input strings.
Example 1
- Input: string1 = "Hello", string2 = "World"
- Output: "HelloWorld"
- Explanation:
- Step 1: Determine the length of both input strings.
- Step 2: Allocate memory for the result string (length of string1 + length of string2 + 1 for null terminator).
- Step 3: Use pointers to copy characters from string1 to the result string.
- Step 4: Use pointers to append characters from string2 to the result string.
- Step 5: Add null terminator at the end of the result string.
Example 2
- Input: string1 = "C++", string2 = "Programming"
- Output: "C++Programming"
- Explanation:
- Step 1: Determine the length of both input strings.
- Step 2: Allocate memory for the result string (length of string1 + length of string2 + 1 for null terminator).
- Step 3: Use pointers to copy characters from string1 to the result string.
- Step 4: Use pointers to append characters from string2 to the result string.
- Step 5: Add null terminator at the end of the result string.
Constraints
- 1 ≤ len(string1) ≤ 10^3
- 1 ≤ len(string2) ≤ 10^3
- Strings contain printable ASCII characters
- Time Complexity: O(n + m) where n and m are the lengths of the two strings
- Space Complexity: O(n + m) for storing the result
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use pointers to traverse both strings.
- Allocate memory for the concatenated string dynamically.
- Copy characters from both strings to the new string using pointers.