Toggle each Character in a String

Definition & Explanation

Toggling each character in a string involves changing the case of each alphabetical character in the string. Specifically, uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase, while non-alphabetical characters remain unchanged. Here’s the explanation of how this operation works:

  1. Input String: Take a string as input.
  2. Iterate Through Characters: Iterate through each character in the string.
  3. Check Character Case:
    • If the character is an uppercase alphabetical character (A-Z), convert it to lowercase.
    • If the character is a lowercase alphabetical character (a-z), convert it to uppercase.
    • If the character is not an alphabetical character, leave it unchanged.
  4. Construct Toggled String: As you iterate through each character, construct a new string with the toggled characters.
  5. Output Toggled String: Output the final toggled string.

Program Logic

  • In the input string “Hello, World!”, uppercase characters ‘H’ and ‘W’ are converted to lowercase, while lowercase characters ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘o’, ‘r’, ‘l’, ‘d’ are converted to uppercase. The non-alphabetical characters (‘,’, ‘ ‘) remain unchanged. Therefore, the toggled string becomes “hELLO, wORLD!”.

Implementing this logic in code involves iterating through the characters of the input string and applying the case conversion operation based on the character’s type. The specific implementation may vary slightly depending on the programming language used.


Method 1 :

#include <stdio.h>

void toggleString(char *str) {
    while (*str) {
        if (*str >= 'A' && *str <= 'Z')
            *str += ('a' - 'A');
        else if (*str >= 'a' && *str <= 'z')
            *str -= ('a' - 'A');

int main() {
    char str[] = "Hello, World!";
    printf("Toggled String: %s\n", str);
    return 0;

Output :


Method 1 :

#include <iostream>
using namespace std;

void toggleString(string &str) {
    for (char &c : str) {
        if (c >= 'A' && c <= 'Z')
            c += ('a' - 'A');
        else if (c >= 'a' && c <= 'z')
            c -= ('a' - 'A');

int main() {
    string str = "Hello, World!";
    cout << "Toggled String: " << str << endl;
    return 0;

Output :


Method 1 :

public class ToggleString {
    public static String toggleString(String str) {
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c))
            else if (Character.isLowerCase(c))
        return result.toString();

    public static void main(String[] args) {
        String str = "Hello, World!";
        String toggledString = toggleString(str);
        System.out.println("Toggled String: " + toggledString);

Output :


Method 1 :

def toggle_string(input_str):
    toggled_str = ''
    for char in input_str:
        if char.isupper():
            toggled_str += char.lower()
        elif char.islower():
            toggled_str += char.upper()
            toggled_str += char
    return toggled_str

input_str = "Hello, World!"
toggled_str = toggle_string(input_str)
print("Toggled String:", toggled_str)

Output :

Toggled String: hELLO, wORLD!