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

More than 3 years have passed since last update.

【C++】大文字と小文字の入れ替えを関数化してみるなど

Last updated at Posted at 2021-02-22

この記事は今後修正の予定があります。ご了承ください。

背景

大文字と小文字をさくっと入れ替えたい。
文字列に大文字が含まれているかさくっと判別したい。
そんなときにさくっと使える関数があるといいなと思い、作りました。

作った関数

bool capital_judge(const char c)

入力した文字が大文字か小文字かを判定します。

入力 return
A〜Z true
a〜z false
それ以外 プログラム終了
capital_judge(const_char)
/*  関数名          capital_judge(const char c)
    説明            入力が大文字なら true, 小文字なら false を返す
    使用ライブラリ  なし
    制約事項        入力が大文字、小文字のどちらでもなければプログラムを終了させます。
*/
bool capital_judge(const char c){
    if('a'<=c && c<='z') return false;
    else if('A'<=c && c<='Z') return true;
    else std::exit(0);
}

bool str_cap_judge(const string str)

文字列に大文字が含まれているかどうかを判定します。

入力 return
A〜Z を 1 つ以上含む true
A〜Z を 1 つも含まない false
A〜Z、a〜z 以外の文字を1つでも含む プログラム終了

A〜Z を 1 つ以上含み、かつ A〜Z、a〜z 以外の文字を 1 つでも含む場合はプログラム終了が優先されます。

str_cap_judge(const_string)
/*  関数名          str_cap_judge(const string str)
    説明            入力文字列に大文字が含まれていれば true, 含まれていなければ false を返す
    使用ライブラリ  string
    制約事項        入力にアルファベット以外の文字が含まれていた場合は、プログラムを終了させます。
*/
bool str_cap_judge(const string str){
    for(int i=0; i<str.length(); i++){
        if('A'<=str[i] && str[i]<='Z') return true;
        else if(str[i]<'a' || 'z'<str[i]) std::exit(0);
    }
    return false;
}

char capital_conv(const char c)

入力がした文字が大文字なら小文字に、小文字なら大文字にして返します。

入力 return
A〜Z a〜z
a〜z A〜Z
それ以外 プログラム終了
capital_conv(const_char)
/*  関数名          capital_conv(const char c)
    説明            入力が大文字なら小文字に、小文字なら大文字に変換します。
    使用ライブラリ  なし
    制約事項        入力が大文字、小文字のどちらでもなければプログラムを終了させます。
*/
char capital_conv(const char c){
    if('a'<=c && c<='z') return c-32;
    else if('A'<=c && c<='Z') return c+32;
    else std::exit(0);
}

コード中の32という定数は、ASCIIコードにおける大文字と小文字の差です。

ASCII - Wikipedia
https://ja.wikipedia.org/wiki/ASCII

'A' のアスキー値が 65、'a' のアスキー値が 97 なので、ちょうどこの差の 32 を足し引きすることで大文字と小文字を変換できます。


string str_cap_conv(const string str)

文字列の大文字と小文字を入れ替えます。
アルファベット以外の文字は、そのままにします。

入力例 return
AbCdEFgh aBcDefGH
A1b!C@d9 a1B!c@D9
str_cap_conv(const_string)
/*  関数名          str_cap_conv(const string str)
    説明            文字列の大文字と小文字を入れ替えます
    使用ライブラリ  string
    制約事項        アルファベット以外の文字に対しては何もしません
*/
string str_cap_conv(string str){
    for(int i=0; i<str.length(); i++){
        if('a'<=str[i] && str[i]<='z') str[i] -= 32;
        else if('A'<=str[i] && str[i]<='Z') str[i] += 32;
    }
    return str;
}

定数 32 の意味は先程と同じです。

使い方

こんな感じで使います。

capital.cpp
# include<iostream>
# include<string>

using namespace std;

bool capital_judge(const char c);
bool str_cap_judge(const string str);
char capital_conv(const char c);
string str_cap_conv(string str);

int main(){
    char c1 = 'A', c2 = 'a';
    string str1 = "aBc", str2 = "abc";

    cout << capital_judge(c1)   << ", " << capital_judge(c2)   << endl;
    cout << str_cap_judge(str1) << ", " << str_cap_judge(str2) << endl;
    cout << capital_conv(c1)    << ", " << capital_conv(c2)    << endl;
    cout << str_cap_conv(str1)  << ", " << str_cap_conv(str2)  << endl;

    return 0;
}
出力結果
1, 0
1, 0
a, A
AbC, ABC

まとめ

大文字と小文字の変換や抽出は、ASCII値で処理してしまえば簡単ですね。
少し改変するだけで文字列から大文字or小文字のみを抽出したり、アルファベットと数字以外のときにエラー文字or文字列をリターンしたり無視したり、簡単に拡張できそうです。

0
0
11

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?