Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chuyển chuỗi sang chữ HOA trong C++

Chuyển đổi string sang uppercase trong C++ có nhiều cách, từ C-style đến C++ modern.

C-style (char array) - ASCII math

#include <iostream>
#include <cstring>
#include <cctype> // Khuyến nghị

void toUpperAscii(char* str) {
    for (int i = 0; str[i]; i++) {
        if (str[i] >= 'a' && str[i] <= 'z')
            str[i] -= 32; // 'a' - 'A' = 32
    }
}

void toUpperCtype(char* str) {
    for (int i = 0; str[i]; i++) {
        str[i] = std::toupper((unsigned char)str[i]);
    }
}

int main() {
    char s[] = "This_is_string";
    toUpperCtype(s);
    std::cout << "Uppercase: " << s << '\n'; // THIS_IS_STRING
    return 0;
}

C++ std::string + Algorithm (Khuyến nghị)

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string s = "This_is_string";
    
    // In-place transform
    std::transform(s.begin(), s.end(), s.begin(),
        [](unsigned char c) { return std::toupper(c); });
    
    std::cout << "Uppercase: " << s << '\n'; // THIS_IS_STRING
    
    // Tạo bản copy mới
    std::string upper = s;
    std::transform(upper.begin(), upper.end(), upper.begin(),
        [](unsigned char c) { return std::toupper(c); });
    
    return 0;
}

C++17: std::string_view + algorithm (Zero-copy view)

#include 
#include 
#include 

std::string toUpperCopy(std::string_view sv) {
    std::string result;
    result.reserve(sv.size());
    std::transform(sv.begin(), sv.end(), std::back_inserter(result),
        [](unsigned char c) { return std::toupper(c); });
    return result;
}

C++20/23: Ranges (Tương lai)

#include 
#include 
#include 

// C++20: views::transform
auto upper_view = str | std::views::transform([](char c) { return std::toupper(c); });
// Cần materialize: std::string(upper_view.begin(), upper_view.end())

Locale-aware conversion

#include 
#include 

int main() {
    std::string s = "straße"; // German sharp S
    
    // Classic locale (C locale) - chỉ ASCII
    std::string upper_c;
    std::transform(s.begin(), s.end(), std::back_inserter(upper_c),
        [](unsigned char c) { return std::toupper(c, std::locale::classic()); });
    std::cout << "C locale: " << upper_c << '\n'; // STRASSE
    
    // User locale (hệ thống)
    std::locale loc("");
    std::string upper_loc;
    std::transform(s.begin(), s.end(), std::back_inserter(upper_loc),
        [&loc](unsigned char c) { return std::toupper(c, loc); });
    std::cout << "User locale: " << upper_loc << '\n'; // STRASSE (hoặc ẞ)
    
    return 0;
}

So sánh phương pháp

Lưu ý

  • Luôn cast (unsigned char) khi dùng toupper/tolower để tránh UB với char signed
  • ASCII math chỉ đúng cho A-Z/a-z. Không hoạt động với accented chars (é, ñ, ü...)
  • C++: ưu tiên std::transform + lambda
  • Turkish locale: 'i' → 'İ' (dotted), 'I' → 'ı' (dotless) - khác ASCII
Phương phápƯu điểmNhược điểm
ASCII math (c-=32)Nhanh, không dependencyChỉ ASCII, broken cho non-ASCII
std::toupper (C locale)Chuẩn, portableChỉ ASCII cơ bản
std::toupper (locale)Unicode-aware (partial)Phụ thuộc locale hệ thống
ICU / Boost.LocaleĐầy đủ UnicodeThư viện ngoài, nặng