0% found this document useful (0 votes)
16 views

#Include #Include #Include #Include #Include

This document contains C++ code that implements three different approaches to converting a string to uppercase: string replacement, character offset, and using the toupper function. The main function gets a string from the user and tests each conversion approach, timing each operation over three trials and outputting the results. Helper functions are defined for each conversion approach. The code is intended to practice different ways of performing uppercase conversion and compare their performance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

#Include #Include #Include #Include #Include

This document contains C++ code that implements three different approaches to converting a string to uppercase: string replacement, character offset, and using the toupper function. The main function gets a string from the user and tests each conversion approach, timing each operation over three trials and outputting the results. Helper functions are defined for each conversion approach. The code is intended to practice different ways of performing uppercase conversion and compare their performance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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