๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program that uses functions to insert a sub-string into a given main string from a given position.

โ† All Lab Programs

Write a C program that uses functions to insert a sub-string into a given main string from a given position.

C Program
//insert a sub-string into a given main string from a given position.
#include <stdio.h>
#include <string.h>

int main()
{
    char str[100], sub[50], result[150];
    int pos;

    printf("Enter the main string: ");
    scanf(" %[^\n]", str);

    printf("Enter the substring: ");
    scanf(" %[^\n]", sub);

    printf("Enter the position: ");
    scanf("%d", &pos);

    strncpy(result, str, pos);  // Copy string up to position
    strcat(result, sub);      // Insert substring
    strcat(result, str + pos);  // Append remaining part

    printf("Modified string: %s\n", result);

    return 0;
}

OUTPUT
Enter the main string: welcome to Glance
Enter the substring: Study
Enter the position to insert the substring: 11
Modified string: welcome to StudyGlance 

๐Ÿ’ป Run This C Program

Write a C program that uses functions to insert a sub-string into a given main string from a given position.
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals