Monday, 15 August 2016

Delete the substring of length n from the string at specified position (pos)

Delete the substring of length n from the string at specified position (pos)

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

void main()
{
          char str[20], substr[20];
          int n, pos, i, m;
          printf("\nEnter the string:");
          gets(str);
          printf("\nEnter the position:");
          scanf("%d", &pos);
          printf("\nEnter the number of characters to be deleted:");
          scanf("%d", &n);
          m = strlen(str);

          for(i=pos;i<m-n;i++)
          { 
              str[i] = str[i+n];
          }
          str[i] ='\0';
          printf("\nThe substring is:");
          puts(str);
}

Output:
Enter the string:         abcdefghijkl
Enter the position:     3
Enter the number of characters to be deleted:  5
The substring is:     abcijkl