{ }codelper
string indexingcharactersP0053

First and last position of a character

The first line gives a string s and the second line gives a single character c.
Print the position where c first appears in s and the position where it last appears, separated by one space.
Positions count from 0.
If c never appears in s, print "-1 -1".
If it appears exactly once, both numbers are the same.

Input
s: lowercase letters and digits, length 1 to 1,000.
c: one lowercase letter or digit
Output
the first and last position (-1 -1 if absent)
Example 1 · stdin
banana
a
stdout
1 5
Example 2 · stdin
hello
z
stdout
-1 -1
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

First and last position: Python s.find(c)/s.rfind(c), JS/TS/Java indexOf/lastIndexOf, C# IndexOf/LastIndexOf (ordinal when given a char), C++ find/rfind, C strchr/strrchr.
Craykon gets the first position with s.IndexOf(c) and finds the last with a loop from the end asking s[i] == c (there is no search-from-the-back member).
What you get when it's absent differs: usually -1, but Python index/rindex raise ValueError, and C++ find returns std::string::npos (the largest size_t, 18446744073709551615), so printing it directly does not give -1.
C's strchr returns a pointer or NULL: check for NULL first, then the position is p - s.
A single character is a char (single quotes, 'a') in Java, C#, C and C++; Python, JS, TS and Craykon have no separate type, so it's a string of length 1.

Pythonmain.py · 3 lines
s = input()c = input()[0]print(s.find(c), s.rfind(c))
JavaScriptmain.js · 3 lines
const [s, line] = require("fs").readFileSync(0, "utf8").split("\n").map((x) => x.trim());const c = line[0];console.log(s.indexOf(c), s.lastIndexOf(c));
TypeScriptmain.ts · 4 lines
import { readFileSync } from "node:fs";const [s, line]: string[] = readFileSync(0, "utf8").split("\n").map((x) => x.trim());const c: string = line[0];console.log(s.indexOf(c), s.lastIndexOf(c));
JavaMain.java · 11 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();        char c = br.readLine().trim().charAt(0);        System.out.println(s.indexOf(c) + " " + s.lastIndexOf(c));    }}
C#Main.cs · 11 lines
using System; class Program{    static void Main()    {        string s = Console.ReadLine().Trim();        char c = Console.ReadLine().Trim()[0];        Console.WriteLine(s.IndexOf(c) + " " + s.LastIndexOf(c));    }}
C++main.cpp · 12 lines
#include <iostream>#include <string> int main() {    std::string s;    char c;    std::cin >> s >> c;    size_t first = s.find(c);    size_t last = s.rfind(c);    std::cout << (first == std::string::npos ? -1 : (long long)first) << " "              << (last == std::string::npos ? -1 : (long long)last) << "\n";}
Cmain.c · 12 lines
#include <stdio.h>#include <string.h> int main(void) {    char s[1001];    char c;    if (scanf("%1000s %c", s, &c) != 2) return 0;    char *first = strchr(s, c);    char *last = strrchr(s, c);    printf("%d %d\n", first ? (int)(first - s) : -1, last ? (int)(last - s) : -1);    return 0;}
Craykonmain.cray · 13 lines
s = Console.ReadLine().Trim();c = Console.ReadLine().Trim()[0];first = s.IndexOf(c);last = -1;for (i = s.Length - 1; i >= 0; i--){    if (s[i] == c)    {        last = i;        break;    }}Console.WriteLine(first + " " + last);