Menu
Coddy logo textTech

Swap Cases

Lesson 10 of 18 in Coddy's Functions in C++: Building Your Own Functions course.

In programming, characters are represented as codes known as ASCII codes. Each character has a specific code. For example:

  • The ASCII code for 'a' is 97.
  • The ASCII code for 'b' is 98, and so on.

It’s important to note that the ASCII codes for lowercase letters differ from those for uppercase letters. For example:

  • The ASCII code for 'A' is 65.
  • The ASCII code for 'B' is 66.

The difference between the ASCII codes of lowercase and uppercase letters is 32.

challenge icon

Challenge

Easy

Write a function that swaps the cases of characters.


The function swap_cases will take a string as a parameter and return a new string with the cases of the characters swapped: lowercase characters will be changed to uppercase, and uppercase characters will be changed to lowercase.

For example:

  • If the input is: "CoDDy"
  • The output will be: "cOddY"

Try it yourself

#include <iostream>
#include <string>

using namespace std;

string swap_cases(string str) {
   
}

All lessons in Functions in C++: Building Your Own Functions