String Concatenation Safe

String Concatenation Safe

Hard C Strings 35 views
Explanation Complexity

Problem Statement

Append source to destination string. But check for buffer overflow first. Safe implementation of alert().

Input Format

Two strings:

destination string

source string
Also the maximum size of the destination buffer.

Output Format

Destination string after appending source
OR
Error message if buffer overflow may happen.

Example

Destination = "Hello"
Source = "World"
Buffer size = 11
HelloWorld

Constraints

• Destination buffer has a fixed size

• Appending must not exceed buffer size

Concept Explanation

Before appending, the program checks if destination has enough space to hold the source string.
If not, appending is stopped to avoid buffer overflow.

Step-by-Step Explanation

1.Take destination string, source string, and buffer size.

2.Find the length of destination string.

3.Find the length of source string.

4.Check:

• dest_length + src_length + 1

Concept Explanation

Before appending, the program checks if destination has enough space to hold the source string.
If not, appending is stopped to avoid buffer overflow.

Step-by-Step Explanation

1.Take destination string, source string, and buffer size.

2.Find the length of destination string.

3.Find the length of source string.

4.Check:

• dest_length + src_length + 1

Input / Output Format

Input Format
Two strings:

destination string

source string
Also the maximum size of the destination buffer.
Output Format
Destination string after appending source
OR
Error message if buffer overflow may happen.
Constraints
• Destination buffer has a fixed size

• Appending must not exceed buffer size

Examples

Input:
Destination = "Hello" Source = "World" Buffer size = 11
Output:
HelloWorld

Example Solution (Public)

C
#include <stdio.h>

void safe_append(char dest[], int destSize, const char src[]) {
    int i = 0, j = 0;

    // Find length of destination
    while (dest[i] != '�') {
        i++;
    }

    // Find length of source
    while (src[j] != '�') {
        j++;
    }

    // Check for buffer overflow
    if (i + j + 1 > destSize) {
        printf("Error: Buffer overflow prevented. Append not allowed.n");
        return;
    }

    // Append source to destination
    j = 0;
    while (src[j] != '�') {
        dest[i] = src[j];
        i++;
        j++;
    }

    dest[i] = '�';   // null terminate
}

int main() {
    char destination[20] = "Hello";
    char source[] = " World";

    safe_append(destination, sizeof(destination), source);

    printf("Resulting string: %sn", destination);

    return 0;
}

Official Solution Code

#include <stdio.h>

void safe_append(char dest[], int destSize, const char src[]) {
    int i = 0, j = 0;

    // Find length of destination
    while (dest[i] != '�') {
        i++;
    }

    // Find length of source
    while (src[j] != '�') {
        j++;
    }

    // Check for buffer overflow
    if (i + j + 1 > destSize) {
        printf("Error: Buffer overflow prevented. Append not allowed.n");
        return;
    }

    // Append source to destination
    j = 0;
    while (src[j] != '�') {
        dest[i] = src[j];
        i++;
        j++;
    }

    dest[i] = '�';   // null terminate
}

int main() {
    char destination[20] = "Hello";
    char source[] = " World";

    safe_append(destination, sizeof(destination), source);

    printf("Resulting string: %sn", destination);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.