단어 뒤집기

Seung jun Cha·2022년 10월 17일
0

1. StringBuilder를 사용하여 뒤집기

  • reverse() : StringBuilder 에 들어온 값을 reverse() 함수로 뒤집어준다. 이때 값타입은 StringBuilder이므로 .toString을 사용하여 String 타입으로 변환한다.
public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String toString = "";
        ArrayList<String> stringList = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            stringList.add(sc.next());
        }

        for (String s : stringList) {
            toString = new StringBuilder(s).reverse().toString();
            System.out.println(toString);
        }
    }
}

2. for문을 통해 뒤집기

 public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        ArrayList<String> strings = new ArrayList<>();

        String[] str = new String[num];
        

        for (int i = 0; i < num; i++) {
            String next = sc.next();
            str[i] = next;
        }

        for (String s : str) {
            char[] chars = s.toCharArray();
            int lf = 0;
            int rf = s.length() - 1;
            
            while (lf < rf) {
                char tmp = chars[lf];
                chars[lf] = chars[rf];
                chars[rf] = tmp;
                lf++;
                rf--;
            }
            String reverse = String.valueOf(chars);
            strings.add(reverse);
        }
        System.out.println(strings);
    }

3. 재귀함수를 사용하기

public static String reverseString(String str) {
	    if (str.length() == 0) {
	        return str;
	    } else {
	        return reverseString(str.substring(1)) + str.charAt(0);
	    }
	}
}

4. 주어진 단어에서 특정문자 뒤집기

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String next = sc.next();

        char[] chars = next.toCharArray();

        int lf = 0;
        int rf = chars.length - 1;

        while (lf < rf) {
            if (!Character.isAlphabetic(chars[lf])) {
                lf++;
            } else if (!Character.isAlphabetic(chars[rf])) {
                rf--;
            } else {
                char tmp;
                tmp = chars[lf];
                chars[lf] = chars[rf];
                chars[rf] = tmp;
                lf++;
                rf--;
            }
        }
        String reverse = String.valueOf(chars);
        System.out.println(reverse);
    }
public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        String result = "";


        String token = st.nextToken();
        char[] chars = token.toCharArray();

        int lf = 0;
        int rf = chars.length - 1;

        while (lf < rf) {
            if (!Character.isAlphabetic(chars[lf])) {
                lf++;
            } else if (!Character.isAlphabetic(chars[rf])) {
                rf--;
            } else {
                char x = chars[lf];
                chars[lf] = chars[rf];
                chars[rf] = x;
                lf++;
                rf--;
            }
        }
        result = String.valueOf(chars);
        System.out.println(result);
    }

0개의 댓글