1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

大文字小文字の反転

Last updated at Posted at 2025-05-15

今日やったのは Aa にして pAIZAPaiza に戻すみたいな割とシンプルな問題。大文字小文字の反転問題について解説!


問題概要

大文字と小文字のアルファベットが混ざった文字列 S が与えらる。
S の小文字を全て大文字に、大文字を全て小文字にした文字列を出力せよ。


入力例:

Paiza

出力例:

pAIZA


✅コード例

const rl = require('readline').createInterface({input:process.stdin});

rl.once('line',(input)=>{
     // 入力された文字列を1文字ずつの配列に変換する
    const text = input.split('');

    // 大文字・小文字を反転した結果を格納する配列
    const flippedCase = [];

    // 文字ごとに処理を行う
    text.forEach(char => {

        // 文字が大文字の場合(toUpperCaseしても変わらない)
        if (char.toUpperCase() === char) {

            // 小文字に変換して追加
            flippedCase.push(char.toLowerCase());
        }

        // 文字が小文字の場合(toLowerCaseしても変わらない)
        else if (char.toLowerCase() === char) {

            // 大文字に変換して追加
            flippedCase.push(char.toUpperCase());
        }
    });

    // 配列を文字列に戻して出力
    console.log(flippedCase.join(''));
})
  • 文字列を配列に変換 input.split''):1文字ずつ処理できるようにする
  • 大文字か小文字か判定 :char.toUpperCase() === char などで判定
  • 大文字↔小文字変換 :toLowerCase() / .toUpperCase() を使って反転
  • 出力 console.log() で反転後の文字列を表示

🔍 ポイントは「変換前と変換後が一致するかどうかの比較」

例として、char = “A” のとき:

char === char.toUpperCase()  true(=もともと大文字だった

逆に char = “a” のとき:

char === char.toUpperCase()  false(=もともと小文字だった

つまり、

  • char === char.toUpperCase() が true → 大文字だった

  • char !== char.toUpperCase() が true → 小文字だった

という「変換しても変わらなかったかどうか」を見て、判別する仕組み。

if (char === c.toUpperCase()) {
    // 変換しても変わらない → 大文字
} else {
    // 変換された → 小文字
}

気づきメモ

  • .toUpperCase().toLowerCase() は「変換」だけじゃなく、「判定」にも使える!
  • char === char.toUpperCase() の発想が新鮮だった。


まとめ

とりあえず変換して、比べてみる!




僕の失敗談(´;ω;`)と解決法🐈

1
0
2

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?