Coding's core task involves manipulating strings, and changing the letter case within a string is a common operation. In this guide, we will explore how to develop a C++ program to toggle between uppercase and lowercase in a string.
Identifying the Problem:
Switching the cases of a string involves converting uppercase letters to lowercase and vice versa. The goal is to maintain non-alphabetical characters while producing a modified string where each character has the opposite case compared to the original string.
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 consider a C++ program that switches the cases of characters in a string:
#include <iostream>
#include <string>
using namespace std;
string toggleCases(const string &input) {
string result = "";
for (char c: input) {
if (isalpha(c)) {
if (isupper(c)) {
result += tolower(c);
} else {
result += toupper(c);
}
} else {
result += c;
}
}
return result;
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
string toggled = toggleCases(input);
cout << "Original string: " << input << endl;
cout << "String with cases toggled: " << toggled << endl;
return 0;
}
Output:
Explanation:
- A function called toggleCases is defined, which accepts an input string and outputs the string with the cases toggled .
- After that, we initialize an empty string result inside the function to store the changed string.
- Next, we employ a for loop to cycle through each character in the input string.
- Using isalpha , we determine whether the character is an alphabet character.
- If the letter is uppercase, we use tolower to convert it to lowercase before appending it to the output string.
- If the letter is lowercase, we use toupper to convert it to uppercase before appending it to the output string.
- The character is appended to the result string if it is not an alphabetic character.
- Lastly, we toggle the cases and return the result string.
- 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 consider a different C++ program to switch between uppercase and lowercase characters in a string:
#include <iostream>
#include <string>
using namespace std;
string toggleCases(const string &input) {
string result = input; // Make a copy of the input string
for (char &c : result) {
if (isalpha(c)) {
// Use XOR to toggle the case of alphabet characters
c = isupper(c) ? tolower(c) : toupper(c);
}
}
return result;
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
string toggled = toggleCases(input);
cout << "Original string: " << input << endl;
cout << "String with cases toggled: " << toggled << endl;
return 0;
}
Output:
Explanation:
- In this program, the input/output and string manipulation header files are included at the start of the program.
- 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.
- 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.
- 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.
- 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.
- The result variable contains the altered string with the cases toggled.
- The updated string is returned by the toggleCases
- 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.
- Toggling the cases in the input string requires calling the toggleCases function with the input string as an argument.
- After that, using cout, the program shows the updated string (with cases toggled) in addition to the original string.
- When the program finally returns 0, it has successfully executed.
- A copy of the input string is made and kept in a variable named result to make sure the original string is kept.
- Character after character, the loop iterates and processes each one separately.
- 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.
- 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.
The process of changing the letter case of alphabetical characters within a specified string while leaving non-alphabetical characters untouched is thoroughly elucidated through the rationale provided in this context.