温馨提示×

如何在C++中使用toupper函数进行字符串分割

c++
小樊
82
2024-09-10 21:23:07
栏目: 编程语言

在C++中,toupper函数用于将小写字母转换为大写字母。要在C++中使用toupper函数对字符串进行分割,您需要遍历字符串并将每个字符转换为大写。以下是一个示例:

#include<iostream>
#include<string>
#include <cctype> // 包含 toupper 函数所需的头文件

int main() {
    std::string input = "Hello, World!";
    std::string output = "";

    for (char c : input) {
        output += std::toupper(c); // 将当前字符转换为大写并添加到输出字符串
    }

    std::cout << "Original string: "<< input<< std::endl;
    std::cout << "Uppercase string: "<< output<< std::endl;

    return 0;
}

这个程序首先包含了<cctype>头文件,以便使用toupper函数。然后,它遍历输入字符串中的每个字符,并使用toupper函数将其转换为大写。最后,将转换后的大写字符添加到输出字符串中。

请注意,这个示例仅演示了如何使用toupper函数将字符串中的所有字符转换为大写。如果您希望根据某个条件(例如空格或逗号)对字符串进行分割,您可能需要使用其他方法,如std::istringstreamstd::string::find

0