๐Ÿ’ป
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 delete n Characters from a given position in a given string.

โ† All Lab Programs

Write a C program that uses functions to delete n Characters from a given position in a given string.

C Program
//delete n Characters from a given position in a given string.
#include <stdio.h>
#include <string.h>

int main() {
    char str[20],finalStr[20];
    int position,n;

    // Input a string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);  

    // Input the position
    printf("Enter the position from where you want to delete: ");
    scanf("%d", &position);

    // Input the no.of characters to delete    
    printf("Enter the number of characters to delete: ");
    scanf("%d", &n);

    // Check if the position is valid
    int strLen = strlen(str);
    if (position < 0 || position > strLen) {
        printf("Invalid position.\n");
        return 1;
    }
    strncpy(finalStr,str,position);   // Copy string up to position
    finalStr[position] = '\0';     // Important null terminate
    strcat(finalStr,str+position+n);  // Concatenate remaining characters
  
    // Print the modified string
    printf("Modified string: %s\n", finalStr);

    return 0;
}

OUTPUT
Enter a string: Study at a Glance
Enter the position from where you want to delete: 5
Enter the number of characters to delete: 6
Modified string: StudyGlance

๐Ÿ’ป Run This C Program

Write a C program that uses functions to delete n Characters from a given position in a given string.
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