0
0

More than 1 year has passed since last update.

Bランクレベルアップメニュー 文字列の重複カウント Java 解答

Last updated at Posted at 2022-10-16

STEP: 30 文字列の長さ

問題

解答

step.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        
        // 文字数を取得
        int length = line.length();
        System.out.println(length);
        
        sc.close();
    }
}

結果

image.png

STEP: 31 文字列の1文字目

問題

解答

step31.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        displayFirstCharacter(sc);
        sc.close();
    }
    
    /**
     * 1文字目を表示する関数
     * 
     * @param sc 標準入力
     * */
    public static void displayFirstCharacter(Scanner sc){
        
        // 文字目を区切る
        sc.useDelimiter("");
        String firstLetter = sc.next();
        System.out.println(firstLetter);
    }
}

結果

image.png

STEP: 32 1文字ずつ出力

問題

解答

step32.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        outputOneCharacterAtTime(sc);
        sc.close();
    }
    
    /**
     * 1文字ずつ出力する関数
     * @ param sc 標準入力
     * */
    public static void outputOneCharacterAtTime(Scanner sc){
        // ""で文字を区切る
        sc.useDelimiter("");
        while(sc.hasNext()){
            System.out.println(sc.next());
        }
    }
}

結果

image.png

STEP: 33 文字列の1、2文字目

問題

解答

step33.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        // 半角スペースで区切る
        sc.useDelimiter("");
        String firstLetter = sc.next();
        String secondLetter = sc.next();

        System.out.println(firstLetter + " " + secondLetter);
        sc.close();
    }
    

}

結果

image.png

STEP: 34 文字列の n 文字目と n + 1 文字目

問題

解答

step34.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        // n 文字目を指定する変数(配列は0から始まる為)
        int n = sc.nextInt();

        String line = sc.next();
        String answer = outputNthCcharacterPlusNPlus1stCharacter(n, line);
        System.out.println(answer);
       // test();
        sc.close();
    }
    
    /**
     * n 文字目と n 足す1 文字目出力する関数
     * 
     * @param n 開始文字
     * @param line 対象文字列
     * 
     * @return 結果
     * */
    public static String outputNthCcharacterPlusNPlus1stCharacter(int n, String line){
     
        String answer = "";
        if(line.length() > 1 && n < line.length()){
            String part = line.substring(n-1, n+1);
            String[] split = part.split("");
            answer = split[0] + " " + split[1];
        }
        
        return answer;
    }
    
    private static List<List<String>> testLists = Arrays.asList(
            Arrays.asList("1","abcd","a b"),
            Arrays.asList("3","abcd","c d"),
            Arrays.asList("4","abcd",""),
            Arrays.asList("10","abcd","")
        );
        
    private static void test(){
        

        for(List<String> testList : testLists){
            int n = Integer.valueOf(testList.get(0));
            String line = testList.get(1);
            String answer = outputNthCcharacterPlusNPlus1stCharacter(n, line);
            if(Objects.equals(answer, testList.get(2))){
                 System.out.println("OK:" + answer);
            } else {
                 System.out.println("NG:" + answer);
            }
        }
    }
}

結果

image.png

FINAL問題 文字列の重複カウント

問題

解答

final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String t = sc.nextLine();
        int answer = getMatchCount(s, t);
        System.out.println(answer);
        //test();
        sc.close();
    }
    
    /**
     * 一致する文字の数を数える関数
     * 
     * @param s 検索文字列
     * @param t 検索対象文字列
     * 
     * @return 結果
     * */
    public static int getMatchCount(String s, String t){
        String searchString = t;
        int count = 0;
        int index = -1;
        do{

            // 一致するインデックスを返却
            index = searchString.indexOf(s);
            if(index != -1){
                count++;
                // 1文字ずつ
                searchString = searchString.substring(index + 1);
            }

        }while(index != -1); 
        
        return count;
        
    }

    private static List<List<String>> testLists = Arrays.asList(
        Arrays.asList("AA","abdeeAAbAAAbfde", "3"),
        Arrays.asList("el","scale", "0"),
        Arrays.asList("Ji","JiJiiJiIjiIJjIJi", "4")
        );
        
    private static void test(){
        for(List<String> testList : testLists){
            String s = testList.get(0);
            String t = testList.get(1);
            int answer = getMatchCount(s, t);
            if(answer == Integer.valueOf(testList.get(2))){
                System.out.println("OK:" + answer);
            }else{
                System.out.println("NG:" + answer);               
            }
        }
    }
}

結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0