Light Blue Pointer
본문 바로가기
Coding Test

[프로그래머스][Lv.0]특수문자 출력하기

by Greedy 2023. 10. 31.

문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/181948?language=java

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

다음과 같이 출력하도록 코드를 작성해 주세요.


출력 예시

!@#$%^&*(\\'"<>?:;

 

제출 코드

1. Java

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        System.out.print("!@#$%^&*(\\\\'\\"<>?:;");
    }
}

 

 

2. Python

print("!@#$%^&*(\\'\"<>?:;")

 

풀이과정

1. Java

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        System.out.println("!@#$%^&*(\\'"<>?:;");
    }
}

중간에 “ 이후는 인식이 안 됨

<>?:;"

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        System.out.printf("!@#$%^&*(\\'");
        System.out.printf(""+'"');
        System.out.printf("<>?:;");
    }
}

이렇게 쪼개보았는데 안 된다

⭐Java Char to string

Character.toString()

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        System.out.printf("!@#$%^&*(\\'");
        System.out.printf(Character.toString('"'));
        System.out.printf("<>?:;");
    }
}

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '^'

‘”’이거 자체가 잘못됐나봄

⭐How to print double quotes in Java

  1. Type the escape character \.

As you know, the double quote symbol " has a special meaning in Java (displaying text). Whenever you want to ignore one of these meanings, use the escape character \ (backslash). This character tells the compiler that the next character is part of an alternate instruction.

**\\"**
  1. Use ASCII Code

Use char(34) to represent double quotes.

Java can easily represent ASCII symbols using the char type.

34 is the ASCII code for the " symbol, so write char(34) to display " without using its special meaning.

  • You can look up a symbol's ASCII code by searching for an ASCII code table online.
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        System.out.print("!@#$%^&*(\\\\'\\"<>?:;");
    }
}

통과했다!

2. Python

print("!@#$%^&*(\\'\\"<>?:;")

“뒤로 인식이 또 안 되길래 아까 자바 풀때 본 내용으로 백슬래시 붙여봄

그랬더니 백슬래시가 출력이 안 됨 ㅋㅋㅋㅋㅋㅋ

"이 문제는 입력이 없습니다." 기댓값 〉 "!@#$%^&(\'"<>?:;" 실행 결과 〉 실행한 결괏값 !@#$%^&('"<>?:;이 기댓값 !@#$%^&(\'"<>?:;과 다릅니다. 출력 〉 !@#$%^&('"<>?:;

백슬래시 앞에도 백슬래시 붙여서 해결

print("!@#$%^&*(\\'\"<>?:;")