Een C++-tekenreeks naar hoofdletters converteren

Ik moet een string in C++ converteren naar volledige hoofdletters. Ik ben al een tijdje aan het zoeken en heb een manier gevonden om het te doen:

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

Helaas werkte dit niet en kreeg ik deze foutmelding:

geen overeenkomende functie voor aanroep naar ‘transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,

Ik heb andere methoden geprobeerd die ook niet werkten. Dit kwam het dichtst in de buurt van werken.

Dus wat ik vraag is wat ik verkeerd doe. Misschien is mijn syntaxis slecht of moet ik iets toevoegen. Ik weet het niet zeker.

Ik heb de meeste van mijn informatie hier:
http://www.cplusplus.com/forum/beginner/75634/
(laatste twee berichten)


Antwoord 1, autoriteit 100%

Je moet een dubbele dubbele punt plaatsen voor toupper:

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

Uitleg:

Er zijn twee verschillende toupper-functies:

  1. toupperin de globale naamruimte (toegankelijk met ::toupper), die afkomstig is van C.

  2. toupperin de stdnaamruimte (toegankelijk met std::toupper) die meerdere overbelastingen heeft en dus niet eenvoudig kan worden verwezen met alleen een naam. Je moet het expliciet casten naar een specifieke functiehandtekening om ernaar te kunnen verwijzen, maar de code voor het verkrijgen van een functieaanwijzer ziet er lelijk uit: static_cast<int (*)(int)>(&std::toupper)

Aangezien je using namespace stdgebruikt, verbergt 2. bij het schrijven van toupper1. en wordt dus gekozen volgens de regels voor naamresolutie.


Antwoord 2, autoriteit 17%

Boost string-algoritmen:

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

Een tekenreeks in C++ converteren naar hoofdletters


Antwoord 3, autoriteit 15%

Probeer dit kleine programma, rechtstreeks van C++-referentie

#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


Antwoord 4, autoriteit 5%

Je zou kunnen doen:

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

Antwoord 5, autoriteit 2%

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

Antwoord 6

Hoofdletters naar kleine letters en omgekeerd met BitWise-operators

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: kijk voor meer info op deze link


Antwoord 7

U kunt ook de functie uit onderstaande code gebruiken om deze om te zetten in hoofdletters.

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

Other episodes