From ‘A’ and ‘a’ to ‘Z’ and ‘z’

Difficulty: ★ ☆ ☆ ☆

Here’s an easy exercise to try, something for those first learning C programming but also a challenge for experienced users: Output the alphabet A to Z in both upper and lowercase letters. It’s an “easy” exercise only when you don’t explore the possibilities.

The output string looks like this:

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz

An obvious and easy solution is the following statement:

printf ("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz\n";

Because I’m showing this solution in the post, you can’t use it. No, you must probably employ some type of loop. Remember, if you’re a beginner, this exercise can be a challenge. For experienced C programmers, I believe the true challenge is being creative with your solution.

Here’s a hint: On the ASCII table, the difference between lowercase and uppercase character codes is 32.

I’ve created five solutions to this Exercise. Each one uses a different approach, from the simple and obvious to the convoluted and insane. Please try the challenge on your own before you peek to see what I’ve done.

Leave a Reply