LoginSignup
0
0

More than 3 years have passed since last update.

備忘録No.3「入力された文字列を条件によって、文字ごとに入れ替え」[Java]

Last updated at Posted at 2020-10-28

はじめに

いわゆるLeet表記作成

やりたい事

1.文字列を一つ入力(字数は自由)
2.文字ごとに判定し、変換を行う
3.変換された文字列を出力

コード

        public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();

        List<Character>list = new ArrayList<>();
        for(int i = 0; i < s.length(); i++) {
            list.add(s.charAt(i));//入力した文字列を一文字ずつCharacter型のリストに追加
        }

        for(int i = 0; i < s.length(); i++) {
            char c = list.get(i);
            switch(c) {
            case 'A':
                list.set(i, '4');
                break;

            case 'E':
                list.set(i, '3');
                break;

            case 'G':
                list.set(i, '6');
                break;

            case 'I':
                list.set(i, '1');
                break;

            case 'O':
                list.set(i, '0');
                break;

            case 'S':
                list.set(i, '5');
                break;

            case 'Z':
                list.set(i, '2');
                break;
            }
        }

        for(char word : list) {
            System.out.print(word);
        }
    }
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