What Does Subtracting a Char From a Char Mean?
By: Tremaine Eto
Tags: Software Development, Coding, Technology, Software Engineering, Programming

Actually, they kind of are variables

However, for this example, each char is kind of a variable. What I mean by that is that b and a in this example actually can be represented by ASCII number values summarized by the below chart:

Public domain.

If you look on the right-hand side there, you’ll see that b corresponds to a decimal value of 98 and a corresponds to a decimal value of 97 . When you subtract the characters a from b , then, you get a result of 1 ! With this, the below code block (written in Java, but the concept applies to many other languages) makes sense:

In that case, the output would be Wow! The value of charSubtractionResult and integerSubtractionResult are both 1! .

Uppercase and lowercase characters are different

Something to keep in mind is that the uppercase and lowercase values for an English letter are different. a , which is lowercase, has the aforementioned value of 97 . Its uppercase counterpart, A , though, has a decimal value of 65 ! When you’re doing math like this, remember that it’s case-sensitive.

…Why would you ever need to do this?

That’s a fair question. Aside from “impressing” others — emphasis on the quotation marks there — this is actually something I’ve seen in code where developers are trying to keep track of how many characters are in a character array. It’s probably best I give an example to illustrate this:

So basically, I created an array of integers of length 26 . All the integers’ values are set to 0 by default. The purpose of this array is to keep a count of how many instances of each letter in the English alphabet there are in the string I’ve defined, favoriteFood , which I’ve set the value of to pasta .

In the loop, I want to go through the string and then populate the array with the count for each character. So, for pasta , the array I want would look like this:

[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0]

// a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

Basically, in the string pasta , there is one instance of the character p , two of the character a , one of the character s , and finally one of the character t .

However, the way that I’m trying to access the value to increment is wrong, and it’s because the favoriteFood.charAt(i) simply returns the character at the position. If the program tries to access the index of a character, then it’ll fail — rather, it needs to access the index of an integer.

So subtract a char from a char to get an integer!

You read my mind: that’s where the ASCII character mapping comes into play. Here’s the correct code:

This is actually a clever way of getting the index of the character’s count in the array. Let’s go through the string pasta to illustrate what I mean.

First, we have the letter p , which from the ASCII table above we know has a character value of 112 . When we subtract a , or 97 , we have a value of 112-97=15 .

When the program accesses the 15th index of the charCounts array, that is literally the count of how many p characters there are in the string since p is the 16th character of the alphabet! This is a little tricky because remember that array indexes always start at 0. Thus, there are sixteen indices from 0 to 15.

Let’s keep going to drive this point home. a has a value of 97 , and when we subtract a , or 97 , from it, we’re left with 0 . charCounts[0] correctly corresponds with the number of instances of a in the string.