How to Convert a C++ String to Uppercase

前端 未结 6 1736
不知归路
不知归路 2021-02-01 05:34

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:

#include 
#include 

        
相关标签:
6条回答
  • 2021-02-01 06:00

    Try this small program, straight from C++ reference

    #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

    0 讨论(0)
  • 2021-02-01 06:01
    #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;
    }
    
    0 讨论(0)
  • 2021-02-01 06:15

    You need to put a double colon before toupper:

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

    Explanation:

    There are two different toupper functions:

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

    2. toupper in the std namespace (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)

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

    0 讨论(0)
  • 2021-02-01 06:16

    Boost string algorithms:

    #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

    0 讨论(0)
  • 2021-02-01 06:16

    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));
    }
    
    0 讨论(0)
  • 2021-02-01 06:16

    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.
    }
    
    0 讨论(0)
提交回复
热议问题