{ }codelper
substringsP0054

Cut from the a-th to the b-th character

The first line gives a string s and the second line gives the number of queries q.
Each of the next q lines gives two numbers a and b.
For each query print, on its own line, the part of s from the a-th character to the b-th character, including both ends.
In this problem positions count from 1, the way people count.
For example, if s is "codelper" and a = 1, b = 4, the answer is "code".

Input
s: lowercase letters and digits, length 1 to 1,000.
1 ≤ q ≤ 1,000.
1 ≤ a ≤ b ≤ length of s
Output
q lines, the cut string for each query
Example 1 · stdin
codelper
3
1 4
5 8
3 3
stdout
code
lper
d
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

The second argument of the cutting function means different things.
End position (excluded): Python s[a-1:b], JS/TS s.slice(a - 1, b), Java s.substring(a - 1, b).
Length: C# s.Substring(a - 1, b - a + 1), C++ s.substr(a - 1, b - a + 1), Craykon s.Substring(a - 1, b - a + 1).
C has no cutting function: print from a start pointer with a length, printf("%.*s", b - a + 1, s + a - 1).
Three traps: (1) if you don't convert the 1-based positions to 0-based indexes (s[a:b]), everything shifts by one.
(2) Passing a length to Java substring or an end position to C# Substring gives a different string or an out-of-range exception.
(3) C++ substr silently cuts to the end when the length is too big, so the bug is easy to miss (Python slices also clip silently).
JS substr(start, length) is a legacy function to avoid, and substring differs from slice in that it swaps its arguments if they're reversed.

Pythonmain.py · 5 lines
s = input()q = int(input())for _ in range(q):    a, b = map(int, input().split())    print(s[a - 1:b])
JavaScriptmain.js · 7 lines
const [s, q, ...rest] = require("fs").readFileSync(0, "utf8").split("\n").map((x) => x.trim());const out = [];for (let i = 0; i < Number(q); i++) {  const [a, b] = rest[i].split(" ").map(Number);  out.push(s.slice(a - 1, b));}console.log(out.join("\n"));
TypeScriptmain.ts · 8 lines
import { readFileSync } from "node:fs";const [s, q, ...rest]: string[] = readFileSync(0, "utf8").split("\n").map((x) => x.trim());const out: string[] = [];for (let i = 0; i < Number(q); i++) {  const [a, b]: number[] = rest[i].split(" ").map(Number);  out.push(s.slice(a - 1, b));}console.log(out.join("\n"));
JavaMain.java · 17 lines
import java.io.BufferedReader;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String s = br.readLine().trim();        int q = Integer.parseInt(br.readLine().trim());        StringBuilder out = new StringBuilder();        for (int i = 0; i < q; i++) {            String[] p = br.readLine().trim().split(" ");            int a = Integer.parseInt(p[0]), b = Integer.parseInt(p[1]);            out.append(s.substring(a - 1, b)).append('\n');        }        System.out.print(out);    }}
C#Main.cs · 19 lines
using System;using System.Text; class Program{    static void Main()    {        string s = Console.ReadLine().Trim();        int q = int.Parse(Console.ReadLine());        var out_ = new StringBuilder();        for (int i = 0; i < q; i++)        {            string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            int a = int.Parse(p[0]), b = int.Parse(p[1]);            out_.Append(s.Substring(a - 1, b - a + 1)).Append('\n');        }        Console.Write(out_);    }}
C++main.cpp · 13 lines
#include <iostream>#include <string> int main() {    std::ios::sync_with_stdio(false);    std::string s;    int q, a, b;    std::cin >> s >> q;    for (int i = 0; i < q; i++) {        std::cin >> a >> b;        std::cout << s.substr(a - 1, b - a + 1) << '\n';    }}
Cmain.c · 12 lines
#include <stdio.h> int main(void) {    char s[1001];    int q, a, b;    if (scanf("%1000s %d", s, &q) != 2) return 0;    for (int i = 0; i < q; i++) {        scanf("%d %d", &a, &b);        printf("%.*s\n", b - a + 1, s + a - 1);    }    return 0;}
Craykonmain.cray · 9 lines
s = Console.ReadLine().Trim();q = Console.ReadLine().ToNumber();for (i = 0; i < q; i++){    ab = Console.ReadNumbers();    a = ab[0];    b = ab[1];    Console.WriteLine(s.Substring(a - 1, b - a + 1));}