1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
/**
* kccasing.cpp
* Simple string and character recasing functions.
**
* Author: Kyle Campbell (k.j.campbell@clasnet.sunyocc.edu)
*/
#ifndef KCS_CASING
#define KCS_CASING
#include <string>
using namespace std;
// This file is part of the KC Shared function library.
namespace kcs
{
/**
* Function Prototypes
**
* lowerCase(c/s): Change the case of the character or an entire string to lower.
* switchCase(c/s): Switch the case of the character, or each character in a string.
* upperCase(c/s): Change the case of the character or an entire string to upper.
**
* String notes:
* Passing an additional argument of true to any of these functions will tell
* the function to only manipulate the casing of the first character in the
* string.
*/
void lowerCase(char& c, bool first_char_only = false);
void lowerCase(string& s, bool first_char_only = false);
void switchCase(char& c, bool first_char_only = false);
void switchCase(string& s, bool first_char_only = false);
void upperCase(char& c, bool first_char_only = false);
void upperCase(string& s, bool first_char_only = false);
/**
* Function Declarations
*/
void lowerCase(char& c, bool first_char_only)
{
if (c >= 'A' && c <= 'Z')
c = ((c - 'A') + 'a');
}
void lowerCase(string& s, bool first_char_only)
{
if (first_char_only)
{
lowerCase(s[0]);
return;
}
else
{
// Save s.length() in a variable so were not calling it every loop.
int str_length = int(s.length());
for (int i = 0; i < str_length; i++)
lowerCase(s[i]);
}
}
void switchCase(char& c, bool first_char_only)
{
if (c >= 'a' && c <= 'z')
c = ((c - 'a') + 'A');
else if (c >= 'A' && c <= 'Z')
c = ((c - 'A') + 'a');
}
void switchCase(string& s, bool first_char_only)
{
if (first_char_only)
{
switchCase(s[0]);
return;
}
else
{
// Save s.length() in a variable so were not calling it every loop.
int str_length = int(s.length());
for (int i = 0; i < str_length; i++)
switchCase(s[i]);
}
}
void upperCase(char& c, bool first_char_only)
{
if (c >= 'a' && c <= 'z')
c = ((c - 'a') + 'A');
}
void upperCase(string& s, bool first_char_only)
{
if (first_char_only)
{
upperCase(s[0]);
return;
}
else
{
// Save s.length() in a variable so were not calling it every loop.
int str_length = int(s.length());
for (int i = 0; i < str_length; i++)
upperCase(s[i]);
}
}
}
#endif
|