[HackerRank]-Java Strings Introduction
일곱번 째 해커랭크 문제 기초이다.
Sample Input
hello
java
Sample Output
9
No
Hello Java
예시 설명
2개의 문자열 A, B 입력을 한다.
문자열 수의 합을 출력하고
A와 B를 비교해서 사전적 크기 비교후 A가 크면 Yes 아니면 No출력한다.
A와 B를 한문장에 출력하고 맨앞은 대문자로 바꾼다.
코드
import java.io.*;
import java.util.*;
public class Solution {
static String firstUp(String s){
String a = "";
for(int i=0;i<s.length();i++){
if(i==1) a = a.toUpperCase();
a += s.charAt(i);
}
return a;
}
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. */
int sum = 0;
sum = A.length() + B.length();
System.out.println(sum);
if(A.compareTo(B) > 0) System.out.println("Yes");
else System.out.println("No");
System.out.println(firstUp(A)+" "+firstUp(B));
}
}
설명
sum을 선언후 length()로 문자열 수를 구할 수 있다. 사전적 비교는 compareTo()로 비교가능하다. 새로 firstUp함수를 만들어 앞글자만 toUpperCase()를 이용해 변환해준다.
요약
length()는 문자열 수 구하기. compareTo()는 사전적 크기 비교. toUpperCase()는 소문자를 대문자로 바꾸기. toLowerCase()는 대문자를 소문자로 바꾸기.