Program to Convert String in Upper Case, Lower Case and Toggle Case

Program to Convert String in Upper Case, Lower Case and Toggle Case

  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in C
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in C++
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in Python
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in PHP
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in Java
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in Java Script
  • Write a program to Convert String in Upper Case, Lower Case and Toggle Case in C#

Explanation:

Using string manipulation capabilities found in the majority of programming languages, we may follow easy steps to change a string into uppercase, lowercase, and toggle case. The reasoning behind these conversions is as follows:

Steps:

  • For Uppercase: For each character, if it is between ‘a’ and ‘z’, add the difference (‘A’ – ‘a’) to convert it.
  • For Lowercase: For each character, if it is between ‘A’ and ‘Z’, add the difference (‘a’ – ‘A’) to convert it.
  • For Toggle Case: For each character, check if it’s lowercase or uppercase and switch accordingly.

Program to Convert String in Upper Case, Lower Case and Toggle Case

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void toUpperCase(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

void toLowerCase(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }
}

void toggleCase(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (islower(str[i]))
            str[i] = toupper(str[i]);
        else if (isupper(str[i]))
            str[i] = tolower(str[i]);
    }
}

int main() {
    char str[100];

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    char upperStr[100], lowerStr[100], toggleStr[100];
    strcpy(upperStr, str);
    strcpy(lowerStr, str);
    strcpy(toggleStr, str);

    toUpperCase(upperStr);
    toLowerCase(lowerStr);
    toggleCase(toggleStr);

    printf("Uppercase: %s", upperStr);
    printf("Lowercase: %s", lowerStr);
    printf("Togglecase: %s", toggleStr);

    return 0;
}

#include <iostream>
#include <cctype>
using namespace std;

void toUpperCase(string &str) {
    for (char &ch : str) {
        ch = toupper(ch);
    }
}

void toLowerCase(string &str) {
    for (char &ch : str) {
        ch = tolower(ch);
    }
}

void toggleCase(string &str) {
    for (char &ch : str) {
        if (islower(ch))
            ch = toupper(ch);
        else if (isupper(ch))
            ch = tolower(ch);
    }
}

int main() {
    string str;

    cout << "Enter a string: ";
    getline(cin, str);

    string upperStr = str, lowerStr = str, toggleStr = str;

    toUpperCase(upperStr);
    toLowerCase(lowerStr);
    toggleCase(toggleStr);

    cout << "Uppercase: " << upperStr << endl;
    cout << "Lowercase: " << lowerStr << endl;
    cout << "Togglecase: " << toggleStr << endl;

    return 0;
}

def to_upper_case(string):
    return string.upper()

def to_lower_case(string):
    return string.lower()

def toggle_case(string):
    return string.swapcase()

string = input("Enter a string: ")
print("Uppercase:", to_upper_case(string))
print("Lowercase:", to_lower_case(string))
print("Togglecase:", toggle_case(string))

<?php
function toUpperCase($string) {
    return strtoupper($string);
}

function toLowerCase($string) {
    return strtolower($string);
}

function toggleCase($string) {
    $result = '';
    for ($i = 0; $i < strlen($string); $i++) {
        $ch = $string[$i];
        if (ctype_lower($ch)) {
            $result .= strtoupper($ch);
        } elseif (ctype_upper($ch)) {
            $result .= strtolower($ch);
        } else {
            $result .= $ch;
        }
    }
    return $result;
}

echo "Enter a string: ";
$string = trim(fgets(STDIN));

echo "Uppercase: " . toUpperCase($string) . "\n";
echo "Lowercase: " . toLowerCase($string) . "\n";
echo "Togglecase: " . toggleCase($string) . "\n";
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = scanner.nextLine();

        String upperCase = str.toUpperCase();
        String lowerCase = str.toLowerCase();
        String toggleCase = toggleCase(str);

        System.out.println("Uppercase: " + upperCase);
        System.out.println("Lowercase: " + lowerCase);
        System.out.println("Togglecase: " + toggleCase);
    }

    private static String toggleCase(String str) {
        StringBuilder result = new StringBuilder();
        for (char ch : str.toCharArray()) {
            if (Character.isLowerCase(ch)) {
                result.append(Character.toUpperCase(ch));
            } else if (Character.isUpperCase(ch)) {
                result.append(Character.toLowerCase(ch));
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }
}

function toUpperCase(str) {
    return str.toUpperCase();
}

function toLowerCase(str) {
    return str.toLowerCase();
}

function toggleCase(str) {
    let result = '';
    for (let ch of str) {
        if (ch === ch.toLowerCase()) {
            result += ch.toUpperCase();
        } else if (ch === ch.toUpperCase()) {
            result += ch.toLowerCase();
        } else {
            result += ch;
        }
    }
    return result;
}

const str = prompt("Enter a string:");
console.log("Uppercase:", toUpperCase(str));
console.log("Lowercase:", toLowerCase(str));
console.log("Togglecase:", toggleCase(str));

using System;

class Program {
    static void Main() {
        Console.Write("Enter a string: ");
        string str = Console.ReadLine();

        Console.WriteLine("Uppercase: " + str.ToUpper());
        Console.WriteLine("Lowercase: " + str.ToLower());
        Console.WriteLine("Togglecase: " + ToggleCase(str));
    }

    static string ToggleCase(string str) {
        char[] result = new char[str.Length];
        for (int i = 0; i < str.Length; i++) {
            char ch = str[i];
            if (char.IsLower(ch)) {
                result[i] = char.ToUpper(ch);
            } else if (char.IsUpper(ch)) {
                result[i] = char.ToLower(ch);
            } else {
                result[i] = ch;
            }
        }
        return new string(result);
    }
}

List of All Programs