2일차 알고리즘 문자열

쿠우·2022년 7월 15일

설명
N개의 단어가 주어지면 각 단어를 뒤집어 출력하는 프로그램을 작성하세요.

입력
첫 줄에 자연수 N(3<=N<=20)이 주어집니다.
두 번째 줄부터 N개의 단어가 각 줄에 하나씩 주어집니다. 단어는 영어 알파벳으로만 구성되어 있습니다.

출력
N개의 단어를 입력된 순서대로 한 줄에 하나씩 뒤집어서 출력합니다.

  1. 입력부분을 만들어야한다.
    -입력을 몇 번 받을지 정하는 메서드
    -입력을 받는 메서드
  2. 반복문을 통해 정리
  3. 입력받은 부분을 뒤집는 메서드 에서 출력

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		String strarr = " ";
		int count = intInput();
		
		while(20 < count || count < 3) {
			count = intInput();
		}//while 
		
		while(count!=0) {
			strarr += " "+stringInput();
			count--;
		}//while
		
		change(strarr.trim().split(" "));
		
		
	}// main

	private static int intInput() {
		Scanner sc=new Scanner(System.in); 
		int count = sc.nextInt();
		return count;
	}// intInput
	
	private static String stringInput(){
		Scanner sc=new Scanner(System.in); 
		String str  = sc.nextLine();
		return str;
	}// StringInput
	
	private static void change(String[] arr) {
		
		for(String str: arr ) {
			StringBuffer strbf= new StringBuffer(str);
			
			System.out.println(strbf.reverse());
		}// for
		
	}// change
	
}//end class

깨우치거나 느낀점: StringBuffer를 이용을 오랜만에 해봤다.. 하지만.. 이클립스에서는 잘 사용되는데
채점화면에서는 계속 RuntimeException 뜬다.. ㅠ 뭐 때문이지.. 이래저래 계속 시도해봐야겠다..
이걸 1시간 반 넘게 풀고 앉았네.. ArrayList로 쉽게 풀 수 있는데 바보같이 풀었다. 불필요한 부분이 너무 많다.

profile
일단 흐자

1개의 댓글

comment-user-thumbnail
2022년 7월 15일

그냥 입력 부분을 하나로 해서 써야겠다..

답글 달기