[JAVA][HackerRank] 30 Days of code Day 6


Day 6: Let’s Review


Task Given a string, , of length that is indexed from to , print its even-indexed and odd-indexed characters as space-separated strings on a single line (see the Sample below for more detail).

Note: is considered to be an even index.

Input Format

The first line contains an integer, (the number of test cases). Each line of the subsequent lines contain a String, .

Constraints

  • 1 <= T <= 10
  • 2<= length of S <= 10000

Output Format

For each String (where ), print ‘s even-indexed characters, followed by a space, followed by ‘s odd-indexed characters.

Sample Input


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
	import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt();
        String str;

        for(int i=0;i<T;i++){
            str = scan.next();

            for(int j=0;j<str.length();j++){

                if(j%2 ==0){
                    System.out.print(str.charAt(j));
                }
            } 
            System.out.print(" ");
            for(int j=0;j<str.length();j++){
                if(j%2==1){
                    System.out.print(str.charAt(j));
                }

            }
            System.out.println();
        }
        
    }
}


}


그냥 막연하게 stdin과 stdout은 입출력이라고 생각하고 있었는데,

아무래도 scanner에 대해서 알아보기 위해 찾아보았다.

System.in을 input값으로 받아 처리해주는 것이 scanner클래스이다.

scan.nextLine() 은 문자열을 받고(한줄)

scan.nextInt()는 정수값을 받아온다.

scan.nextDouble()은 double형을 갖고온다.

scan.next()은 문자열을 받는데 한 글자 단위로 입력받는다.

하지만 scanner클래스는 입력값이 많아질때는 사용을 추천하지 않는다고 한다.

난 그냥 퀴즈니까 간단하게 찍으려고 낸듯!




© 2018. by sora

Powered by sora