Make your own custom C++ ToUpperCase and ToLowerCase functions (Intermediate Level)

Converting between upper case and lower case characters can always be useful, depending on what we want to accomplish. In C++ we normally, include <ctype.h> that provides us with two functions: toupper(); and tolower();

Here’s how we would use them:

#include <iostream>
#include <string>
#include <ctype.h> 

using namespace std; 

void main()
     string s = "mario";
     string ss = "ZELDA"; 
     for(int i = 0; i<s.length(); i++){
	  s[i] = toupper(s[i]); 

     for(int j = 0; j<ss.length(); j++){
          ss[j] = tolower(ss[j]); 

     cout << s << endl; 
     cout << ss << endl; 

Here’s the result when running this code:

That’s very simple, isn’t it? Yeah! I think it’s too simple, but that’s me. However, what about making our own tolower and toupper functions with C++, completely customized functions, this is way more fun! We’ve got a challenge ahead, so let’s solve it.

I’ll be creating two functions both will return string, I name them, ToUpperCase(); and ToLowerCase(); and they both take one string parameter each:

string ToLowerCase(string text);
string ToUpperCase(string text);

To complete these two functions we need to use the American Standard Code for Information Interchange (ASCII) table, which provides a complete list of different characters including the alphabetic ones. In addition, the ASCII table provides the decimal numbers that represent each character. I’ll be using the ACII table found on

According to the ASCII table:

Upper Case: 65-90
Lower Case: 97-122

The ASCII table is correct, if we declare a char c = 65; and print this, we’ll see ‘A’ in the console window, this means that it is printing an upper case ‘A’, since 65 in the table equals ‘A’.

I’ll start by writing the very first function, the ToUpperCase(); function:

string ToUpperCase(string text){
  for(int i = 0; i<text.length(); i++){
     char c = text[i]; 
   return text; 

Once you’ve written this function (or copied it), hit F5 in Visual Studio to run the program in Debug Mode. Don’t forget to invoke the function like below:

string s = "super mario"; 
s = ToUpperCase(s);
cout << s << endl; 

If everything went well, and you didn’t encounter any issues while compiling, you should see this:

Awesome, so it works, we succeeded with the first function. Now lets go in-depth and explain the magic behind the success, yes, I will explain what’s happening when we invoke this function and pass a string to it.

Let’s have a look on our string s = “super mario”; I have added a simplified picture of the string index array below:

We can easily loop through it with a For-loop. So far, so good, things remain clear and easy. A char data type gives us the ASCII decimal numbers that each single character equals to. First we check for lower case letters, if((c>=97)&&(c<=122)), next we do the magic that converts an lower case letter to an upper case letter.

The magic happens right here:


But what happens exactly? Simple, and yet complicated to fully explain, &= is a Bitwise AND operator, remember binary and bits? Yes, to explain what exactly happens here, we have to dive deep into the binary world, yeah! Weird huh?

If we look in our code again, we see that the first single character in our string index array is ‘s’ and in the ASCII table, the decimal value for ‘s’ is 115. The &=0xdf, 0xdf is a hexadecimal value that equals to 223 (decimal).

To understand this, you’ll have to understand the AND gate and use a Truth table remember from the basic electricity courses in school? The AND gate is easy to understand for example, if A = 0 and B = 0 the output becomes 0, and if A = 0 and B = 1 the output becomes 0, the only time the output becomes 1 is if both A & B equals 1. I also marked the zeros with red rectangles to highlight a pattern that simplifies the understanding of this. The number 83 in the ASCII table represents the upper case ‘S’.

Now that I’ve completed the ToUpperCase() function, I will write the next function, ToLowerCase() which converts upper case letters to lower case letters. This is easy, now that we made the first function.

string ToLowerCase(string text){
   for(int i = 0; i<text.length(); i++){
      char c = text[i]; 
   return text; 

There are some differences between this function and the previous one we wrote, for example, we now check for upper case letters, if((c>=65)&&(c<=90)) and the magic, text[i]|=0x20 now uses the Bitwise OR Assignment Operator |= which differs from the previous Bitwise operator.

Once you’ve written this function (or copied it), hit F5 in Visual Studio to run the program in Debug Mode. Don’t forget to invoke the function like below:

string s = "JAK AND DAXTER"; 
s = ToLowerCase(s);
cout << s << endl; 

If everything went well, and you didn’t encounter any issues while compiling, you should see this:

Yeah, again even this function succeeded. However, I want you to understand the magic behind it, so I made another image that explains the Bitwise OR operator:

To better understand this, let’s have look in our code again, we see that the first single character in our string index array is ‘J’ and in the ASCII table, the decimal value for ‘J’ is 74. The &=0x20, 0x20 is a hexadecimal value that equals to 32 (decimal). For upper case ‘J’ to become lower case letter, ‘j’its value must become 106, and that’s what text[i]|=0x20; does. This can easily be explained by the image above.

The Bitwise OR operator is simple to understand, very straight forward, just think of the OR gate, if A = 0 or B = 0, the output becomes 0, and if A = 1 or B = 0, the output becomes 1. The big difference between the OR gate and AND gate is that, if A = 0 or B = 0 then the output becomes a 0, and that’s the only time in the case of the OR gate. For the AND gate, the only time the output becomes 1 is if both A and B equal 1, else it’s simply 0.

Wrapping up, for Now

I’m not quite done with this, I think it’s fun to play around with it, especially the Bitwise operators which allow us to accomplish a lot. I bet there’s someone that can make other functions using the inner code from one of these two functions I made. In addition, I think it’s useful for programmers to sometimes play around with code at this level of difficulty, despite experience.

For now, happy coding!

Computers allow kids to better understand and learn math

Back in 2010 at TED talks, Conrad Wolfram  spoke about math in schools, and teaching kids real math with the help of using computers (Conrad Wolfram: Teaching kids real math with computers) this video was to me very interesting, so I decided to do some study, and also decided to write this article to present some of my findings, ideas and results.

Teaching math in school

Mathematics in school is simple when you first time start doing math and learning it. However, as you grow older, in school you also study higher levels of math, and these levels become challenging in some cases. There are three factors that impact the way you learn math, these are:

  1.   Your ability to quickly calculate and play with numbers
  2.   The way your teacher is teaching math during a math session
  3.   Your own interest in learning and understanding math

Note: Above I only included three factors, but there are a lot more factors that impact the way we learn math.

Math levels

The math is divided in levels, and each level is unique. Basic level is where we all start, and that is obvious, nobody can start at the last level, even Albert Einstein had to start from basic level.

  1. Basic Math
  2. Pre-Algebra
  3. Algebra
  4. Geometry
  5. Algebra 2
  6. Geometry 2
  7. Trigonometry
  8. Pre-Calculus
  9. Calculus
  10. Advanced Calculus

There are probably a few more levels I haven’t included here.

Computers are great calculators

Personal Computers (PCs) are great calculators and are some of the fastest calculators in the world; of course a modern calculator uses the same technology, the Central Process Unit (CPU), one modern and much known CPU is the Intel® processor. The Arithmetic Logic Unit (ALU) is one of the essential and key features in a CPU, which allows the CPU to perform arithmetic operations Addition, Subtraction and so one. Beside the arithmetic operations, the ALU allows the CPU to perform basic logical operations such as ANDORNOTXOR (exclusive or) and so one.

Humans have created a set of CPU instructions that can be used to tell the CPU what to do; this language is called Assembly (ASM). ASM has been around for a long-time, and is common in many areas in the software world. ASM is still used to build boot loaders in Operating System (OS)development. However, the computer sees everything in 1 and 0 or in binary form, but I would like to demonstrate how a CPU-instruction in ASM will add two numbers, I will be using C++ programming and perform inline ASM.

I will be using the x86 (32-bit) CPU registers to store data. The EAX, EBX, ECX and EDX are the four general registers. I will keep this very basic, with a lot of details.

This code may seem advanced, but is quite easy to understand, okay, I’ll explain. Let’s examine what really happens, we jump to the int declaration.


i. I start by declaring three int variables they get a default value zero.

int valueA, valueB, result = 0; 

ii. Jumping one line, valueA equals 5 and valueB equals 10.

valueA = 5; 
valueB = 10; 


iii. Then we enter the inline code of x86 CPU assembly instructions, we use the MOV instruction to move data from valueA to EAX, and we move data from valueB to EDX (Figure A explains the first move).


Figure A: The MOV instruction makes EAX as the destination for the data variable valueA contains. This is exactly the way the MOV instruction moves data, which can be defined like this: MOV destination, data.
iv. Then we perform addition, by using add eax, ebx which means EAX = EAX + EDX so we store the end result in EAX register.

v. Next, we store the data which EAX contains in the result variable, and finally we XOR (clear) the EAX and EDX registers.

This is another example of how a CPU performs arithmetic calculations. I am glad I was able to provide a demonstration and explanation regarding this.

Programming helps understanding and learning math better

Mathematics appears to be a great subject, but as you move further and begin learning and experimenting with higher levels of mathematics things can get really messy. When   equations and formulas including different theories and rules are involved in mathematics, this will directly make things harder for anybody. However, a key solution to better learning math rules, theories, equations, and formulas is by learning basic programming. If every child started learning basic programming at the age of 10 we would never experience math issues in school, no matter how advanced and complex the math would become at least 90% percent or more of all the students would hopefully pass the test, all this thanks to programming. Children do not need to learn Object Oriented Programming (OOP), they only need the basics (understanding code, data-types, and logic if-else statements), and to learn how to program the application so it can receive input (argument) and provide output (print to the screen) nothing else.

Here’s how programming helps students understand and learn math faster and better. If you know programming, you can make a function in your program that calculates for example the slope of a line. But to make this function you have to learn and understand the formula, the rules and how it is used correctly the mathematical way. However, I will provide two examples, one which is very basic and the other one which is a bit complex, but yet easy as well.

Programming example 1:
Build a program that calculates total price per mile a taxi driver, drives a person. Remember: The taxi company has a fixed price which every customer has to pay the fixed price (f) which equals to $20 USD is added to the price $4 USD / mile.

We have three things to keep in mind:

  1. Price per mile.
  2. How much a customer has to pay?
  3. Remember that 1 Mile equals 10 km

Now this is going to be very simple. But it will allow any student to better remember and understand how this works.

This is the equation or formula which I have setup:



T = Total cost.
P = Price
F = Fixed price
X = Mile(s)

Calculation by either hand on paper or writing it in word like this may seem like a long calculation, but is very short.

T(x) = f + px
T(5) =  20 + (4 * 5)
T(5) = 20 + 20
T(5) = 40

Here’s the code in C++:

int T(int x); 

//$4 USD per mile. 
int p = 4; 
//$20 USD.
const int f = 20;  

void main()
   int Total = 0; 
   Total = T(5); //5 miles = 50 km. 
   cout << "The customer has to pay: $" << Total << " USD" << endl; 
   cout << "" << endl; 

int T(int x)
    return f + (p * x);  


Figure B: The computer did the calculation, but I had to program the software so it can do what I want it to do. This allows me to learn more about the rules, and allows me to determine whether my equation or formula works.

Programming example 2:
In this second programming example, we will be looking at the linear equation, slope and m interception with the Y-axis.

We have two things to keep in mind:

  1. Remember the equation: y = mx + b
  2. Remember that b = y-axis  intercept.



M = slope
B = Y-axis intercept
Y = The total value



Find M-value:



m = y2-y1/x2-x1
m = (3-2)/(2-1)
m = 1


y = 1x + b
y = 1x + 1
y = (1 * 2) + 1
y = 3


Test our calculation:
3 = (1 * 2) + 1

Here’s the code in C++:

double Slope(double y1, double y2, double x1, double x2);  
int Y(double m, int x, int b);

double m = 0; 
int x, y, b = 0; 

void main()
    cout << "Find the Y-axis interception value: "; 
    cin >> b ;
    cout << "\n" << endl; 
    cout << "Calculating..." << endl;
    m = Slope(2, 3, 1, 2); 
    y = Y(m, 2, b); 
    cout << "Y = " << y << endl; 
    cout << "" << endl; 

//Note: Slope function works only when two known points are passed in, 
//single point is not supported by this function. 
double Slope(double y1, double y2, double x1, double x2)
    y2 = y2 - y1; 
    x2 = x2 - x1;

    if(y2 == 0 || x2 == 0)
    { return y2; }
      return y2 / x2; 

int Y(double m, int x, int b)
    return (m * x) + b; 


Figure C: Our program calculating the slope and the Y for us.


I personally think that the nation that first applies “teaching programming” in school, and I mean children start doing basic programming at the age of 10 will increase their economy and produce a lot of smart people. However, this is one of the reasons governments all over the world shall demand that this becomes a reality, because programming covers math, logic thinking and much more. Mathematics has not been an easy subject in school, and that is because of the levels and each individual’s ability to play with numbers, to understand equations etc. But for the coming future, I think programming will play a key role in combating the math problem we see in schools in many parts of the world. The time to change is now, so demand for this change today, for a better tomorrow.

Additional information and resources:
Albert Einstein Biography:
The Arithmetic Logic Unit:
Linear Equation:
Slope-Intercept Form:

Math Is Fun: Geometric Sequences and Sums with C# and (Native) C++

Mathematics is really fun sometimes; however, what makes it difficult is that it is a fundamentally different way of thinking—for example, learning languages can be much easier than learning math.

Since I’ve been playing with geometrical sums and sequences I decided to try this out with some computer programming, so I wrote two methods in two different languages, one in C++ and the other one in C#.

I’ll be using the following geometric sum formula:


static double CalcGeoSum(double a1, double a2, int n)
   double k, sum = 0;
   k = a2 / a1;

   if (k <= 1)
       sum = 0; 
       sum = (a1 * (Math.Pow(k, n) - 1)) / (k - 1);

   return sum; 



float CalcGeoSum(float a1, float a2, int n){
   float k, sum = 0.0;
   k = a2/a1; 
   if(k <= 1)
       sum = 0.0; 
       sum = (a1 * (pow(k, n)-1))/(k-1);
   return sum; 

It’s simple to invoke this function in any of the programming languages, just invoke it and pass it the required arguments. See the example below:

Invoke in C#:

static void Main(string[] args)
    Console.WriteLine(Convert.ToString(CalcGeoSum(3, 18, 6)) + "\n\n");

Invoke in C++:

void main(){
    float result = CalcGeoSum(3, 18, 6); 
    cout << result << endl; 

The output when you compile an run one of these code snippets in debug mode will be:

Output in C#: 27993
Output in C++: 27993

For further reading, I recommend MathWorld,