---
title: "Insert a string into another string"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:insert-string-into-string"
source: https://www.bctengineer.com/q/semester_1/computer_programming/insert-string-into-string
site: BCT Engineering
---

# Insert a string into another string

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2067 Asoj, 2+8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/insert-string-into-string
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Describe the limitations of using getchar and scanf functions for reading strings. Write a complete program to insert a string into another string in the location specified by the user. Read the string in main function, pass them to another function along with inserting position and returns the resulting string.

## Short answer

scanf stops at whitespace and may overflow; getchar requires manual termination and buffer control. To insert, copy characters up to the position, append the new string, then append the rest of the original string.

## Answer

#### Limitations of getchar() and scanf() for reading strings
**scanf("%s")**
- Stops reading at the first space, tab or newline, so a string with spaces ("I love Nepal") cannot be read in one call (only "I" is read).
- Does not check the array size; a long input overflows the array (undefined behaviour) unless a width such as `%49s` is given.
- Leaves the newline in the input buffer, which can disturb the next input (`gets`, `getchar`).

**getchar()**
- Reads only one character at a time, so a loop is needed until the newline or EOF.
- The programmer must add the `'\0'` at the end and also check the array limit.
- Characters typed after the loop ends stay in the buffer.

`fgets()` reads a whole line with spaces and checks the size, so it is used below.

#### Program
`insertString(str1, str2, pos, res)` copies the first `pos` characters of str1, then all of str2, then the rest of str1 into `res`, and returns the resulting string. `main()` displays the returned string.

```c
#include <stdio.h>
#include <string.h>

/* insert str2 into str1 at position pos (0-based); returns the result */
char *insertString(char str1[], char str2[], int pos, char res[])
{
    int i, j = 0, k;
    for (i = 0; i < pos; i++)              /* part before position */
        res[j++] = str1[i];
    for (k = 0; str2[k] != '\0'; k++)      /* the new string */
        res[j++] = str2[k];
    for (; str1[i] != '\0'; i++)           /* rest of str1 */
        res[j++] = str1[i];
    res[j] = '\0';
    return res;
}

int main(void)
{
    char s1[100], s2[50], res[150];
    int pos;
    printf("Enter main string: ");
    fgets(s1, sizeof s1, stdin);
    s1[strcspn(s1, "\n")] = '\0';
    printf("Enter string to insert: ");
    fgets(s2, sizeof s2, stdin);
    s2[strcspn(s2, "\n")] = '\0';
    printf("Enter position (0 to %d): ", (int)strlen(s1));
    scanf("%d", &pos);
    if (pos < 0 || pos > (int)strlen(s1)) {
        printf("Invalid position\n");
        return 1;
    }
    printf("Resulting string: %s\n", insertString(s1, s2, pos, res));
    return 0;
}
```

**Sample run** (insert "really " at position 7)

Input (three lines): `I love Nepal`, then `really ` (with a trailing space), then `7`

```text
Enter main string: Enter string to insert: Enter position (0 to 12): Resulting string: I love really Nepal
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a main limitation of scanf("%s") for reading strings?
   A. It stops at the first space, tab or newline, so 'I love Nepal' reads only 'I'
   B. It cannot read letters
   C. It always adds extra spaces
   D. It reads only digits
   Answer: A. %s stops at whitespace so multi-word strings are cut. (Key point: scanf limitation)

2. What happens if scanf("%s") is used without a width on long input?
   A. It truncates safely
   B. It can overflow the array (undefined behaviour)
   C. It reports an error automatically
   D. It asks again
   Answer: B. scanf does not check the array size unless a width like %49s is given. (Key point: Overflow risk)

3. Which is a limitation of getchar() for reading strings?
   A. It reads the whole line at once
   B. It adds '\0' automatically
   C. It reads one character at a time, and the programmer must add '\0' and check the limit
   D. It cannot read letters
   Answer: C. A loop is needed and termination and bounds must be handled manually. (Key point: getchar limitation)

4. How does insertString() build res?
   A. Copies str2, then str1
   B. Copies str1 completely, then str2
   C. Copies str2 at the end of the first pos characters, dropping the rest of str1
   D. Copies first pos characters of str1, then all of str2, then the rest of str1
   Answer: D. The three loops copy the part before pos, the new string, then the remainder. (Key point: Insertion order)

5. Inserting 'really ' at position 7 into 'I love Nepal' gives?
   A. I love Nepalreally 
   B. I love really Nepal
   C. really I love Nepal
   D. I lovereally  Nepal
   Answer: B. The first 7 characters 'I love ' are followed by 'really ' and then 'Nepal'. (Key point: Sample result)

## Related in Computer Programming

- [Compare two strings with function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-compare-function) — Computer Programming, chapter 7
- [Insert value in array at position](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-insert-algorithm) — Computer Programming, chapter 7
- [Insert number at position in array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-insert-number-in-array) — Computer Programming, chapter 11
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×
- [Palindrome string using function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apalindrome-string-function) — Computer Programming, chapter 7
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×

## Related in other subjects

- [Namespace with pass by reference program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Anamespace-pass-by-ref-program) — Object Oriented Programming, chapter 2
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Transfer function of RC low-pass filter](https://www.bctengineer.com/question.md?key=semester_4%2Fsignals_and_systems%3Arc-lowpass-transfer-function) — Signals and Systems, chapter 5, asked 2×
