Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Case Toggling

Updated 21 Oct 2026

This program reads a sentence and prints it with every capital letter changed to a small letter and every small letter changed to a capital. Digits, spaces and punctuation marks stay as they are. The change is made with character comparisons and ASCII arithmetic, not with toUpperCase() or toLowerCase(), and everything is written in the main method.

Definition

In ASCII the capitals A to Z have the codes 65 to 90 and the small letters a to z have the codes 97 to 122. Each small letter is exactly 32 more than its capital.

  • A small letter (ch >= 'a' && ch <= 'z') becomes a capital by (char)(ch - 32). For example a (97) becomes A (65).
  • A capital (ch >= 'A' && ch <= 'Z') becomes a small letter by (char)(ch + 32). For example Z (90) becomes z (122).
  • Any other character is copied unchanged.
  • Hello World 123, ISC-2026! becomes hELLO wORLD 123, isc-2026!.

The cast (char) is needed because ch - 32 is an int.

Algorithm

  1. Read the sentence into str. If it is empty, print INVALID INPUT and stop.
  2. Set res = "" and count = 0.
  3. For every character ch of str:
    • if ch is between 'a' and 'z' (a small letter), add (char) (ch - 32) to res and add 1 to count;
    • otherwise, if ch is between 'A' and 'Z' (a capital), add (char) (ch + 32) to res and add 1 to count;
    • otherwise (digit, space or punctuation) add ch to res unchanged.
  4. Print the toggled sentence res and the number of letters toggled, count.

Program

The comments show the two ASCII rules: a small letter becomes a capital by taking 32 from its ASCII value, and a capital becomes small by adding 32. The for loop in main applies them character by character.

import java.util.Scanner;

/* Program Toggle reads a sentence and changes every uppercase letter to lowercase
   and every lowercase letter to uppercase using ASCII arithmetic. */
public class Toggle
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();                  // the sentence entered by the user
        if (str.length() > 0)
        {
            String res = "";                         // the sentence after toggling
            int count = 0;                           // the number of letters toggled
            // a small letter becomes a capital (ASCII value - 32), a capital becomes small (ASCII value + 32);
            // every other character is copied unchanged
            for (int i = 0; i < str.length(); i++)
            {
                char ch = str.charAt(i);             // character being examined
                if (ch >= 'a' && ch <= 'z')          // small letter
                {
                    res = res + (char) (ch - 32);
                    count++;
                }
                else if (ch >= 'A' && ch <= 'Z')     // capital letter
                {
                    res = res + (char) (ch + 32);
                    count++;
                }
                else                                 // digit, space or punctuation
                    res = res + ch;
            }
            System.out.println("Toggled sentence: " + res);
            System.out.println("Letters toggled = " + count);
        }
        else
            System.out.println("INVALID INPUT");
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
strStringThe sentence entered by the user
resStringThe sentence after toggling, built one character at a time
countintNumber of letters toggled
iintLoop counter over the characters of str
chcharThe character being examined

Sample input and output

Sample 1

Enter a sentence: Hello World 123, ISC-2026!
Toggled sentence: hELLO wORLD 123, isc-2026!
Letters toggled = 13

Sample 2

Enter a sentence: aZ@[`{ 9 zA
Toggled sentence: Az@[`{ 9 Za
Letters toggled = 4

Sample 3

Enter a sentence: computer science
Toggled sentence: COMPUTER SCIENCE
Letters toggled = 15

One value per line

Dry run

Input aZ@[{ 9 zA. This input is chosen because@,[, ` and{` have ASCII codes just outside the letter ranges, so they must not change.

ichRule usedAdded to rescount
0asmall letter, 97 - 32 = 65A1
1Zcapital, 90 + 32 = 122z2
2@not a letter@2
3[not a letter[2
4`not a letter`2
5{not a letter{2
6spacenot a letterspace2
79not a letter92
8spacenot a letterspace2
9zsmall letter, 122 - 32 = 90Z3
10Acapital, 65 + 32 = 97a4

res is Az@[{ 9 Zaandcountis 4, so the program printsToggled sentence: Az@[{ 9 Za and Letters toggled = 4, the same as the real run above.

Variations

  1. Count the cases "Write a program to count the uppercase and lowercase letters of a sentence."
  2. Capitalise each word "Write a program to change the first letter of every word of a sentence to a capital."
  3. Next letter "Write a program to replace every letter of a sentence by the next letter of the alphabet."