* Capitalize first letter - 첫글자만 대문자로 String 문자열 변환 / compareTo() - 문자열 사전순으로 비교
String API: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String API 문서에서 내용 발췌
* compareTo()
public int compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
public int compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
밑줄 친 부분을 요약하면, String 비교는 유니코드 값을 기반으로 하게 되는데,
A.compareTo(B) = 0 이라면, A와 B가 동일함
A.compareTo(B) < 0 이라면, A가 B보다 앞섬.
A.compareTo(B) > 0 이라면, A가 B보다 뒤쳐짐 이다.
* capitalize first letter
앞의 첫 문자 한개만 잘라내어 대문자로 변경해준다. 그리고 index가 1인 부분부터 마지막까지 String을 잘라내어 둘이 연결하면 된다.
A = A.substring(0, 1).toUpperCase() + A.substring(1);
* 예제코드
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
/* Enter your code here. Print output to STDOUT. */
System.out.println(A.length() + B.length());
String answer;
if(A.compareTo(B) >0){ //B가 먼저이면
answer = "Yes";
}else{ //A가 먼저이면
answer = "No";
}
System.out.println(answer);
A = A.substring(0, 1).toUpperCase() + A.substring(1);
B = B.substring(0, 1).toUpperCase() + B.substring(1);
System.out.println(A + " " + B);
}
}
댓글 없음:
댓글 쓰기