HackerRank/JAVA

Java Strings Introduction

예쁜꽃이피었으면 2024. 1. 16. 09:49

https://www.hackerrank.com/challenges/java-strings-introduction/problem?isFullScreen=true

 

Java Strings Introduction | HackerRank

Learn how to declare a string and perform basic operations.

www.hackerrank.com

 

import java.io.*;
import java.util.*;

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. */
        
        int ALength = A.length();
        int BLength = B.length();
                
        System.out.println(ALength+BLength);
       
        if(A.compareTo(B) <= 0){
            System.out.println("No");
        }else if(A.compareTo(B) > 0){
            System.out.println("Yes");
        }


        String AResult = "";
        String BResult = "";
        
        AResult = A.substring(0,1).toUpperCase()+A.substring(1);
        BResult = B.substring(0,1).toUpperCase()+B.substring(1);

        System.out.println(AResult + " "+ BResult);
        
        
    }
}

 

 


String.length()  : 문자열 길이 구하기

String test = "abc";

int test_len = test.length();

syso("test_len : "+ test_len); // => 3

 

substring() : 문자열 자르기

1) public String substring(int startIndex)

2) public String substring(int startIndex, int endIndex)

* 인덱스는 0부터시작한다.

* startindex만있으면 끝까지 읽는다.

toUpperCase() : 대문자로 반환

String test ="abc";

syso(test.toUpperCase()); // => ABC

 

toLowerCase() : 소문자로 반환

String test ="ABC";

syso(test.toLowerCase()); // => abc

compareTo() : 문자열 비교

1) 숫자의 비교 같은 경우는 단순히 크다(1), 같다(0), 작다(-1) 의 관한 결과값을 반환
2) 문자열의 비교 같은 경우는 같다(0), 그 외 양수/음수값 같은 결과를 반환

* 문자열 비교의 경우 아스키코드값을 비교하기 때문에 사전적 정렬로도 사용할 수 있을 것 같다. 

* 문자열 비교의 경우 대소문자를  구분한다. (구분하고 싶지 않다면 compareToIgnoreCase() 사용 )

 

더보기

This exercise is to test your understanding of Java Strings. A sample String declaration:

String myString = "Hello World!"

The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.

Given two strings of lowercase English letters,  and , perform the following operations:

  1. Sum the lengths of  and .
  2. Determine if  is lexicographically larger than  (i.e.: does  come before  in the dictionary?).
  3. Capitalize the first letter in  and  and print them on a single line, separated by a space.

Input Format

The first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.

Output Format

There are three lines of output:

For the second line, write YesNo instead.

Sample Input 0

hello
java

Sample Output 0

9
No
Hello Java

Explanation 0

String  is "hello" and  is "java".

 has a length of , and  has a length of ; the sum of their lengths is .
When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore,  is not greater than  and the answer is No.

When you capitalize the first letter of both  and  and then print them separated by a space, you get "Hello Java".

반응형

'HackerRank > JAVA' 카테고리의 다른 글

Java Substring Comparisons  (0) 2024.01.17
Java Substring  (0) 2024.01.16
Java Date and Time  (0) 2024.01.12
Java Static Initializer Block  (0) 2024.01.12
Java Datatypes  (0) 2024.01.12