- 2067 Asoj · 2+8 marks
Insert a string into another 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.
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.
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
%49sis 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.
#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
Enter main string: Enter string to insert: Enter position (0 to 12): Resulting string: I love really Nepal
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Compare two strings with function · Computer Programming, chapter 7
- Insert value in array at position · Computer Programming, chapter 7
- Insert number at position in array · Computer Programming, chapter 11
- Reverse a string using functions · Computer Programming, chapter 7 · asked 2×
- Palindrome string using function · Computer Programming, chapter 7
- Copy string converting lower to upper using pointer · Computer Programming, chapter 9 · asked 3×
- Namespace with pass by reference program · Object Oriented Programming, chapter 2
- Member function as friend of another class · Object Oriented Programming, chapter 4
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Transfer function of RC low-pass filter · Signals and Systems, chapter 5 · asked 2×