-- 예전 기록/BOJ

[ BOJ ] 27866 : 문자와 문자열 ( BRONZE 5 ) / C, C++, Python, Java

rejo 2023. 9. 22. 13:09

문제

단어 와 정수 가 주어졌을 때, 번째 글자를 출력하는 프로그램을 작성하시오.

입력

첫째 줄에 영어 소문자와 대문자로만 이루어진 단어 가 주어진다. 단어의 길이는 최대 1,000이다.

둘째 줄에 정수 가 주어진다. (1 ≤ i ≤ |S|)

출력

번째 글자를 출력한다.

풀이 과정

S 문장을 저장하고 S의 i 번째 인덱스 문자를 출력한다.

C

#include <stdio.h>

char string[1001];
int main(void) {
    scanf("%s", string);
    int n;
    scanf("%d", &n);
    printf("%c", string[n - 1]);
    return 0;
}

C++

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

string s;
int main(void) {
    cin >> s;
    int n; cin >> n;
    cout << s.at(n-1);
    return 0;
}

Python

s=input();print(s[int(input())-1])

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int n = sc.nextInt();
        System.out.println(s.charAt(n-1));
    }
}