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

How to Convert a C++ String to Uppercase

本文关键字:转换 字符串 C++      更新时间:2023-10-16

我需要将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;
}

不幸的是,这没有起作用,我收到了这个错误消息:

调用'transform(std::basic_string::迭代器,std::asic_string::迭代器,std::basic_streng::迭代器,(没有匹配的函数

我尝试过其他同样无效的方法。这是最接近工作的一次。

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

我在这里得到了我的大部分信息:http://www.cplusplus.com/forum/beginner/75634/(最后两篇文章(

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

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

说明:

有两种不同的toupper功能:

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

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

既然你是using namespace std,那么在写toupper时,2。隐藏1。并因此根据名称解析规则进行选择。

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");

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

直接从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;
}

实时演示

你可以做:

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));
}

使用BitWise运算符从大写到小写,反之亦然

1.

string s = "cAPsLock";
for(char &c: s)
  c = c | ' ';        // similar to: c = tolower(c);
cout << s << endl; // output: capslock
string s = "cAPsLock";
for(char &c: s)
  c = c & ~' ';       // similar to: c = toupper(c);
cout << s << endl; // output: CAPSLOCK

PS:有关更多信息,请查看此链接

#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;
}

正如leemes所说,您可以使用toupper(int(像这样:

void ToUpper(string &str) {
    for (auto beg = str.begin(); beg != str.end(); ++beg) {
        *beg = toupper(*beg);
    }
}

它将遍历str中的每个字符,并将其转换为大写。示例:

int main()
{
    string name;
    cout << "Insert a name: ";
    cin >> name;
    ToUpper(name);
    cout << "Name in upper case: " << name << endl;
}

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

#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.
}