Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

...++ Programming\Act02_08Jul16_CountLetters\ToUpperCase.

cpp
//Practice code, written by Cadungog, LJ
//Uppercase conversion code, with several variants and test cases.
//Rudimentary code timing setup included.
#include
#include
#include
#include

<iostream>
<string>
<cstdlib>
<cctype>

#include "FunctionTimer.h"
//TODO: find a way to time all these operations over and over, or look for a way to
create a timing class.
std::string to_upper_case_replacement(std::string x);
std::string to_upper_case_offset(std::string x);
std::string to_upper_case_function(std::string x);
int main() {
std::string input, output;
std::cout << "Please enter string: ";
getline(std::cin, input);
std::cout << "Uppercased statement (using string replacement) is: ";
output = to_upper_case_replacement(input);
std::cout << output << std::endl;
std::cout << "Operation time, three trials, is: " << funcTime
(to_upper_case_replacement,input) << " " << funcTime(to_upper_case_replacement,
input) << " " << funcTime(to_upper_case_replacement, input) << std::endl;
std::cout << "Uppercased statement (using offset) is: ";
output = to_upper_case_offset(input);
std::cout << output << std::endl;
std::cout << "Operation time, three trials, is: " << funcTime
(to_upper_case_offset,input) << " " << funcTime(to_upper_case_offset, input) <<
" " << funcTime(to_upper_case_offset, input) << std::endl;
std::cout << "Uppercased statement (using function) is: ";
output = to_upper_case_function(input);
std::cout << output << std::endl;
std::cout << "Operation time, three trials, is: " << funcTime
(to_upper_case_function, input) << " " << funcTime(to_upper_case_function,
input) << " " << funcTime(to_upper_case_function, input) << std::endl;

system("pause");
return 0;

std::string to_upper_case_replacement(std::string x) {
std::string lowercase = "abcdefghijklmnopqrstuvwxyz";
std::string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string return_val;

...++ Programming\Act02_08Jul16_CountLetters\ToUpperCase.cpp
std::string::size_type pos;
for (char& c : x) {
pos = lowercase.find(c);
if (pos != std::string::npos)
return_val += uppercase.at(pos);
else
return_val += c;
}
return return_val;
}
std::string to_upper_case_offset(std::string x) {
std::string return_val = x;
int difference = 'A' - 'a';
for (char& c : return_val) {
if (c >= 'a' && c <= 'z')
c += difference;
}
return return_val;
}
std::string to_upper_case_function(std::string x) {
std::string return_val = x;
for (char& c : return_val) {
c = toupper(c);
}
return return_val;
}

You might also like