Javatpoint Logo
Javatpoint Logo

C++ Program to Toggle Cases in a String

Programming's fundamental activity is string manipulation, and switching the case of characters within a string is a frequent process. We'll look at writing a C++ program to switch between cases in a string in this tutorial.

Identifying the Problem:

Transforming uppercase to lowercase and lowercase to uppercase is known as switching the cases of a string. The objective is to preserve non-alphabetical characters while generating a new string for each character in the original string with the opposite case.

The algorithm to Toggle cases in a String:

An easy approach can be used to switch between the cases in a string:

  • First, set up a blank string to hold the outcome.
  • After that, go over every character in the input string one by one.
  • Verify whether each character is an alphabet character.
  • Convert the character to lowercase and add it to the result string if it is an uppercase letter.
  • Convert the character to uppercase and add it to the result string if it is a lowercase letter.
  • Add the character to the result string if it is not one of the alphabet's characters.
  • Once every character in the input string has been processed, carry on with this process.
  • The input string with cases toggled will be included in the result string.

Program 1:

Let's take an C++ program to toggle cases in String:

Output:

C++ Program to Toggle Cases in a String

Explanation:

  1. A function called toggleCases is defined, which accepts an input string and outputs the string with the cases toggled.
  2. After that, we initialize an empty string result inside the function to store the changed string.
  3. Next, we employ a for loop to cycle through each character in the input string.
  4. Using isalpha, we determine whether the character is an alphabet character.
  5. If the letter is uppercase, we use tolower to convert it to lowercase before appending it to the output string.
  6. If the letter is lowercase, we use toupper to convert it to uppercase before appending it to the output string.
  7. The character is appended to the result string if it is not an alphabetic character.
  8. Lastly, we toggle the cases and return the result string.
  9. The user inputs a string, which is then used in the main function to display the original and modified strings after calling the toggleCases function to toggle the cases.

Program 2:

Let's take another C++ program to toggle cases in String:

Output:

C++ Program to Toggle Cases in a String

Explanation:

  1. In this program, the input/output() and string manipulation () header files are included at the start of the program.
  2. Toggling the cases in a given string is defined by the toggleCases It returns a changed string after receiving the input string as an argument.
  3. Within the function toggleCases:
    • A copy of the input string is made and kept in a variable named result to make sure the original string is kept.
  4. After that, the program employs a loop to iterate through each character in the returned string.
    • Character after character, the loop iterates and processes each one separately.
  5. For every character in the string that results:
    • The isalpha function is used by the code to determine whether the character is an alphabet character.
    • After that, it switches the character's case if it is an alphabet character.
    • The code makes use of a ternary (conditional) operator to switch the case. It uses tolower(c) to transform an uppercase character to lowercase. It uses toupper(c) to change it from lowercase to uppercase if it is in uppercase.
  6. The result variable contains the altered string with the cases toggled.
  7. The updated string is returned by the toggleCases
  8. In the main function:
    • The application asks the user to utilize cout to enter a string.
    • Using getline, it reads user input and stores it in the input string.
  9. Toggling the cases in the input string requires calling the toggleCases function with the input string as an argument.
  10. After that, using cout, the program shows the updated string (with cases toggled) in addition to the original string.
  11. When the program finally returns 0, it has successfully executed.

The method by which the code switches the cases of alphabet characters in a given string without altering non-alphabet characters is explained in detail by the reasoning presented here.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA