uppercase string c++ code example

Example 1: convert whole string to uppercase c++

#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string s = "Viet Nam"; 
    transform(s.begin(), s.end(), s.begin(), ::toupper);  //uppercase
    cout << s << endl; 
    return 0; 
}

Example 2: convert all characters in string to uppercase c++

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

Example 3: string to upper c++

std::string data = "This is a sample string.";
// convert string to upper case
std::for_each(data.begin(), data.end(), [](char & c){
c = ::toupper(c);
});

Example 4: toupper c++

int result = toupper(charecterVariable);// return the int that corresponding upper case char
//if there is none then it will return the int for the original input.
//can convert int to char after
char result2 = (char)toupper(variableChar);

Example 5: c++ toupper string

// toupper example (C++)
#include <iostream>       // std::cout
#include <string>         // std::string
#include <locale>         // std::locale, std::toupper

int main ()
{
  std::locale loc;
  std::string str="Test String.\n";
  for (std::string::size_type i=0; i<str.length(); ++i)
    std::cout << std::toupper(str[i],loc);
  return 0;
}

/*
Output:
TEST STRING.
*/

Tags:

C Example