String Copy Manual

String Copy Manual

Medium C Strings 44 views
Explanation Complexity

Problem Statement

strcpy() without using - copying character by character from source to destination including null terminator. Use pointer arithmetic.

Input Format

Two strings:

• source string

• destination string (enough space)

Output Format

Destination string after copying.

Example

Source: "hello"
Destination: "hello"

Constraints

• Destination must have enough space

• Copy must include null terminator ''

• Use pointer arithmetic only

Concept Explanation

Characters are copied one by one from source to destination using pointers, including the null terminator.

Step-by-Step Explanation

1.Create two pointers:

• src pointing to source string

• dest pointing to destination string

2.While the character pointed by src is not '':

• Copy *src to *dest.

• Increment both pointers (src++, dest++).

3.After loop ends, copy the null terminator:

• *dest = ''.

4.Destination string now contains the copied source string

Concept Explanation

Characters are copied one by one from source to destination using pointers, including the null terminator.

Step-by-Step Explanation

1.Create two pointers:

• src pointing to source string

• dest pointing to destination string

2.While the character pointed by src is not '':

• Copy *src to *dest.

• Increment both pointers (src++, dest++).

3.After loop ends, copy the null terminator:

• *dest = ''.

4.Destination string now contains the copied source string

Input / Output Format

Input Format
Two strings:

• source string

• destination string (enough space)
Output Format
Destination string after copying.
Constraints
• Destination must have enough space

• Copy must include null terminator ''

• Use pointer arithmetic only

Examples

Input:
Source: "hello"
Output:
Destination: "hello"

Example Solution (Public)

C
#include <stdio.h>

int main() {
    char str[200];
    int i;
    int count = 0;

    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '�'; i++) {

        // Word start condition
        if (str[i] != ' ' &&
           (i == 0 || str[i - 1] == ' ')) {
            count++;
        }
    }

    printf("Number of words = %dn", count);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    char str[200];
    int i;
    int count = 0;

    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '�'; i++) {

        // Word start condition
        if (str[i] != ' ' &&
           (i == 0 || str[i - 1] == ' ')) {
            count++;
        }
    }

    printf("Number of words = %dn", count);

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

                                        
Please login to submit solutions.