如何将 C++ 字符串转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23418390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:26:15  来源:igfitidea点击:

How to Convert a C++ String to Uppercase

c++stringuppercase

提问by Thomas W.

I need to convert a string in C++ to full upper case. I've been searching for a while and found one way to do it:

我需要将 C++ 中的字符串转换为全大写。我已经搜索了一段时间并找到了一种方法:

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

using namespace std;

int main()
{
    string input;
    cin >> input;

    transform(input.begin(), input.end(), input.begin(), toupper);

    cout << input;
    return 0;
}

Unfortunately this did not work and I received this error message:

不幸的是,这不起作用,我收到了以下错误消息:

no matching function for call to 'transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,

没有匹配的函数调用 'transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,

I've tried other methods that also did not work. This was the closest to working.

我试过其他方法也无效。这是最接近工作的。

So what I'm asking is what I am doing wrong. Maybe my syntax is bad or I need to include something. I am not sure.

所以我要问的是我做错了什么。也许我的语法不好,或者我需要包含一些东西。我不确定。

I got most of my info here: http://www.cplusplus.com/forum/beginner/75634/(last two posts)

我在这里获得了大部分信息:http: //www.cplusplus.com/forum/beginner/75634/(最后两个帖子)

回答by leemes

You need to put a double colon before toupper:

您需要在toupper:之前放一个双冒号:

transform(input.begin(), input.end(), input.begin(), ::toupper);


Explanation:

解释:

There are two different toupperfunctions:

有两种不同的toupper功能:

  1. toupperin the global namespace (accessed with ::toupper), which comes from C.

  2. toupperin the stdnamespace (accessed with std::toupper) which has multiple overloads and thus cannot be simply referenced with a name only. You have to explicitly cast it to a specific function signature in order to be referenced, but the code for getting a function pointer looks ugly: static_cast<int (*)(int)>(&std::toupper)

  1. toupper::toupper来自 C的全局命名空间中(使用 访问)。

  2. toupper在具有多个重载的std命名空间(用 访问std::toupper)中,因此不能仅用名称简单地引用。您必须将其显式转换为特定的函数签名才能被引用,但是获取函数指针的代码看起来很难看:static_cast<int (*)(int)>(&std::toupper)

Since you're using namespace std, when writing toupper, 2. hides 1. and is thus chosen, according to name resolution rules.

由于您是using namespace std,因此在编写toupper时 2. 隐藏 1. 并因此根据名称解析规则被选中。

回答by Michael S Bergin

Boost string algorithms:

Boost字符串算法:

#include <boost/algorithm/string.hpp>
#include <string>

std::string str = "Hello World";

boost::to_upper(str);

std::string newstr = boost::to_upper_copy("Hello World");

Convert a String In C++ To Upper Case

将 C++ 中的字符串转换为大写

回答by yizzlez

Try this small program, straight from C++ reference

试试这个小程序,直接来自C++ 参考

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

using namespace std;

int main()
{
    string s;
    cin >> s;
    std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));
    cout << s;
    return 0;

}

Live demo

现场演示

回答by jordpw

You could do:

你可以这样做:

string name = "john doe"; //or just get string from user...
for(int i = 0; i < name.size(); i++) {
    name.at(i) = toupper(name.at(i));
}

回答by Joshua Fitzgerald

#include <iostream>

using namespace std;

//function for converting string to upper
string stringToUpper(string oString){
   for(int i = 0; i < oString.length(); i++){
       oString[i] = toupper(oString[i]);
    }
    return oString;
}

int main()
{
    //use the function to convert string. No additional variables needed.
    cout << stringToUpper("Hello world!") << endl;
    return 0;
}

回答by Vishk V

You can also use the function from code below to convert it to Upper-case.

您还可以使用下面代码中的函数将其转换为大写。

#include<iostream>
#include<cstring>

using namespace std;

//Function for Converting Lower-Case to Upper-Case
void fnConvertUpper(char str[], char* des)
{
    int i;
    char c[1 + 1];
    memset(des, 0, sizeof(des)); //memset the variable before using it.
    for (i = 0; i <= strlen(str); i++) {
        memset(c, 0, sizeof(c));
        if (str[i] >= 97 && str[i] <= 122) {
            c[0] = str[i] - 32;    // here we are storing the converted value into 'c' variable, hence we are memseting it inside the for loop, so before storing a new value we are clearing the old value in 'c'.
        } else {
            c[0] = str[i];
        }
        strncat(des, &c[0], 1);
    }
}

int main()
{
    char str[20];  //Source Variable
    char des[20];  //Destination Variable

    //memset the variables before using it so as to clear any values which it contains,it can also be a junk value.
    memset(str, 0, sizeof(str));  
    memset(des, 0, sizeof(des));

    cout << "Enter the String (Enter First Name) : ";
    cin >> str; //getting the value from the user and storing it into Source variable.

    fnConvertUpper(str, des); //Now passing the source variable(which has Lower-Case value) along with destination variable, once the function is successfully executed the destination variable will contain the value in Upper-Case

    cout << "\nThe String in Uppercase = " << des << "\n"; //now print the destination variable to check the Converted Value.
}