Section 5e

Mathematics with Characters


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Character Arithmetic

This is a topic you might not expect. After all, how often do you do math with the alphabet? The answer is, not that much. But the answer is not never. There are circumstances where you might need to display a series of letters from say ‘a’ to ‘f’ (consider your University’s grading scheme). You could create an output for each letter, or you could conduct some repeated operations where you simply increase the character’s value. Remember that a character is a special case of an integer where the numbers can represent letters. Since they are still numbers, they can be manipulated as such.

Consider the following simple example. Since you are not working with program repetition yet, this example mechanically steps through the changes. However, once you begin working with repetition, you will find this process very easy.

// display A grades
char ltrGrade = ‘A’;
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numAStudents );
conIO.printEndline();
ltrGrade = (char)( ltrGrade + 1 );

// display B grades
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numBStudents );
conIO.printEndline();
ltrGrade = (char)( ltrGrade + 1 );

// display C grades
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numCStudents );
conIO.printEndline();
ltrGrade = (char)( ltrGrade + 1 );

You can look up the values of the characters at any time but the only reason to do that is to get a relative feeling for how the letters and other characters are numerically related to each other. The numerical representation is called the American Standard Code for Information Interchange, but everyone knows it as ASCII. One of the best tables is at ASCII Table. That said, you must never use ASCII numerical values in your programs. This significantly limits readability, and makes your program unusable as soon as the convention changes.

And with that said, this kind of change has already happened. Officially speaking, programmers have been using a new standard called Unicode for quite some time. However, they conveniently integrated the ASCII system into the Unicode system. Thus, any programmer who hard-coded ASCII values (i.e., wrote the ASCII numerical values) into a program got away with it this time. Next time there is a change in standards, it might not go as well. To repeat, never hard code ASCII values into programs. Use the letter, as shown here.

char startLetter = 97; // BAD, BAD, BAD

char startLetter = ‘a’; // The right way

The best part here is that it is easier to use the letter and it is more readable since the person reviewing your code – and if you program for a living, someone will always be reviewing your code – can know immediately what you meant to program, without having to go to some other reference.

Subtraction is also handy with characters, although again in a narrow way.

If you have to convert a lower case letter to upper case, you could go to the ASCII table, get the numbers, and do some manipulation. However, smarter people always work less and accomplish more.

You only need to know the numerical “distance” between the lower case letter and the beginning of the alphabet, and then add that distance to the beginning of the upper case alphabet. To wit.

// to convert a lower case letter to upper case
upperCaseLetter = (char)( lowerCaseLetter – ‘a’ + ‘A’ );

The reverse works just fine as well.

// to convert an upper case letter to lower case
lowerCaseLetter = (char)( upperCaseLetter – ‘A’ + ‘a’ );

If you do look at the ASCII table you will notice that lower case letters are numerically larger than upper case letters, but for purposes of what you are doing here, it simply does not matter. It does mean that you cannot compare lower case letters to upper case letters to get meaningful information. However, you now know how to convert them to each others’ cases, so you can compare them in a legitimate way. And again, note that this was done with letters, not numbers.

The final touch on this discussion is to identify a way to enumerate letters in a way that might help manage the data. For example, if you need to know which letter is the first, second, tenth, or twentieth in the list, you can simply implement the subtraction, as was shown previously, and here.

difference = (char)( testLetter – ‘a’ + 1 );

This will provide an appropriate number to each letter in the alphabet, starting with one for ‘a’, two for ‘b’, and so on. Later, when you are using arrays, this can be very helpful since you can effectively turn your letters into indices. But the rest of that story comes later . . .

As might have been expected, there is not too much to discuss about using characters as values. However, as you have now read, there are some places where it can help. The next topic will pull the mathematical operations from these first topics together so that you can effectively use these tools in your program development.


Watch this video and write all three programs. Once again, you have an opportunity to learn the concepts along with practing your program-writing skills.