Delete a fragment#8986

Delete a fragment (Editorial)

Concatenate substrings s[0; n – 1] and s[m + 1; \text{len}], where is the length of the input string.

The task can be solved in another way, by copying the substring s[m+1] to position .

Example

In the task, it is necessary to delete the substring . To do this, it is necessary to copy the data starting from the symbol up to and including the null byte into position .

Algorithm implementation

Declare a character array.

char s[1001];

Read the input data.

gets(s);
scanf("%d %d", &n, &m);

Perform the substring copying.

strcpy(s + n, s + m + 1);

Print the result.

puts(s);

Algorithm implementation – C++

#include <iostream>
#include <string>
using namespace std;

int i, n, m;
string s;

int main(void)
{
    getline(cin, s);
    cin >> n >> m;
    s = s.substr(0, n) + s.substr(m + 1);
    cout << s << endl;
    return 0;
}

Java implementation

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        String s = con.nextLine();
        int n = con.nextInt();
        int m = con.nextInt();
        
        s = s.substring(0, n) + s.substring(m+1);
        
        System.out.println(s);    
        con.close();
    }
}

Python implementation

s = input()
a, b = map(int, input().split())
print(s[:a] + s[b+1:])