Convert a String In C++ To Upper Case

后端 未结 30 1964
一个人的身影
一个人的身影 2020-11-22 05:25

How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.

30条回答
  •  庸人自扰
    2020-11-22 05:52

    template
    char* toupper(char (&dst)[size], const char* src) {
        // generate mapping table once
        static char maptable[256];
        static bool mapped;
        if (!mapped) {
            for (char c = 0; c < 256; c++) {
                if (c >= 'a' && c <= 'z')
                    maptable[c] = c & 0xdf;
                else
                    maptable[c] = c;
            }
            mapped = true;
        }
    
        // use mapping table to quickly transform text
        for (int i = 0; *src && i < size; i++) {
            dst[i] = maptable[*(src++)];
        }
        return dst;
    }
    

提交回复
热议问题