Insert a character in a given string

Write a C program to insert a character in a given string

Sample input : jobaupdate //string
                           5 //position to enter the character
                           t //character to insert
Sample output : jobatupdate

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
int main()
{
    char string[20],c;
    int len,n,k;
    printf("Enter the string : ");
    gets(string);
    printf("\nEnter the position to insert character : ");
    scanf("%d",&n);
    k=n;
    printf("\nEnter the character to insert : ");
    scanf("%s",&c);
    len=strlen(string)+1; //total lenghth of the string + 1
    for(int i=len;i>k;i--)
    {
    string[i-1]=string[i-2];
    }
    string[k-1]=c;
    printf("\nNew String : %s",string);
    return 0;
}

OUTPUT:


0 comments