Better way to generate array of all letters in the alphabet

后端 未结 17 1987
说谎
说谎 2020-12-02 10:56

Right now I\'m doing

for (char c = \'a\'; c <= \'z\'; c++) {
    alphabet[c - \'a\'] = c;
}

but is there a better way to do it? Similar

17条回答
  •  伪装坚强ぢ
    2020-12-02 11:51

    This is a fun Unicode solution.

    char[] alpha = new char[26]
    for(int i = 0; i < 26; i++){
        alpha[i] = (char)(97 + i)
    }
    

    This generates a lower-cased version of alphabet, if you want upper-cased, you can replace '97' with '65'.

    Hope this helps.

提交回复
热议问题