0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-10-16

STEP: 25 文字列の長さ

問題

解答

step25.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: 26 文字列の1文字目

問題

解答

step26.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();
  
        System.out.println(firstLetter);
        
        sc.close();
    }
}


結果

image.png

STEP: 27 配列(リスト)の要素の出力

問題

解答

step27.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        
        List<String> list = Arrays.asList("Nara", "Shiga", "Hokkaido", "Chiba");
        outputOneElementAtTime(list);
        
    }
    
    /**
     * 1要素ずつ出力する関数
     * 
     * @param list 出力したい文字列
     * */
    public static void outputOneElementAtTime(List<String> list){
        
        for(String element :list ){
            System.out.println(element);
        }
    }
}

結果

image.png

STEP: 28 1文字ずつ出力

問題

解答

step28.java
import java.util.*;


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

結果

image.png

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

問題

解答

final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String c = sc.nextLine();
        String s = sc.nextLine();
        
        int count = getMatchCount(c, s);
        System.out.println(count);
        
        sc.close();
    }
    
    /**
     * 1文字とマッチ数取得する関数
     * 文字列 s の中に、文字 c が出現する個数
     * @param c 探索文字
     * @param s 探索対象文字列
     * 
     * @return マッチ数
     * */
    public static int getMatchCount(String c, String s){
        int count = 0;
        //""で文字を区切る
        String[] array = s.split("");
        for(String str:array){
            if(Objects.equals(str,c)){
                count++;
            }
        }
        
        return count;
    }
}

結果

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