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

ISC Practicals and Theory - Case Toggling (OOP)

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 stay the same. This version uses a parameterised constructor and a separate method toggleChar() that changes a single character, so toggle() only has to loop over the sentence.

Definition

In ASCII the capitals A to Z are 65 to 90 and the small letters a to z are 97 to 122, so a small letter is 32 more than its capital. A small letter becomes a capital by (char)(ch - 32) and a capital becomes a small letter by (char)(ch + 32). Every other character is unchanged. For example Hello World 123, ISC-2026! becomes hELLO wORLD 123, isc-2026!.

Algorithm

  1. main reads the line with nextLine() into s and creates ob with new Toggle(s). The parameterised constructor stores s in str, sets res to empty and count to 0.
  2. isValid() returns true if str is not empty. Otherwise main prints INVALID INPUT.
  3. toggleChar(ch) returns (char)(ch - 32) for a small letter, (char)(ch + 32) for a capital, and ch itself for any other character.
  4. toggle() visits every position i of str:
    • ch = str.charAt(i) and t = toggleChar(ch).
    • If t differs from ch, the character was a letter, so count increases.
    • t is added to res.
  5. display() prints the toggled sentence and the number of letters toggled.

Program

isValid() checks for an empty line, toggleChar() changes one character, toggle() builds the result, display() prints, and main creates the object and calls them.

import java.util.Scanner;

/* Class Toggle reads a sentence and changes every uppercase letter to lowercase
   and every lowercase letter to uppercase using ASCII arithmetic. */
public class Toggle
{
    String str;      // stores the sentence entered by the user
    String res;      // stores the sentence after toggling
    int count;       // stores the number of letters toggled

    /* Parameterised constructor. Parameter s: the sentence typed by the user.
       Stores it in str and clears the other data members. */
    Toggle(String s)
    {
        str = s;
        res = "";
        count = 0;
    }

    /* Returns true if the line is not empty, otherwise false */
    boolean isValid()
    {
        return str.length() > 0;
    }

    /* Parameter ch: any character. Returns the toggled character: a small letter
       gives the capital (ASCII value - 32), a capital gives the small letter
       (ASCII value + 32); any other character is returned unchanged. */
    char toggleChar(char ch)
    {
        if (ch >= 'a' && ch <= 'z')
            return (char)(ch - 32);
        if (ch >= 'A' && ch <= 'Z')
            return (char)(ch + 32);
        return ch;
    }

    /* Builds res from str one character at a time using toggleChar().
       count counts the letters whose case was changed. */
    void toggle()
    {
        res = "";
        count = 0;
        for (int i = 0; i < str.length(); i++)
        {
            char ch = str.charAt(i);             // character being examined
            char t = toggleChar(ch);             // its toggled form
            if (t != ch)                         // different, so it was a letter
                count++;
            res = res + t;
        }
    }

    /* Prints the toggled sentence and the number of letters toggled */
    void display()
    {
        System.out.println("Toggled sentence: " + res);
        System.out.println("Letters toggled = " + count);
    }

    /* main reads the sentence, creates the object and calls the methods */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine();            // sentence typed by the user
        Toggle ob = new Toggle(s);           // object of class Toggle
        if (ob.isValid())
        {
            ob.toggle();
            ob.display();
        }
        else
            System.out.println("INVALID INPUT");
    }
}

Variable description table

VariableData TypePurpose
strStringData member: the sentence entered by the user
resStringData member: the sentence after toggling
countintData member: number of letters toggled
sStringParameter of the constructor, and the variable in main that holds the typed sentence
chcharParameter of toggleChar(): the character to change; also the character of str being examined in toggle()
tcharThe toggled form of ch returned by toggleChar()
iintLoop counter: position in str
scScannerReads the line from the keyboard in main
argsString[]Command line arguments of main (not used)
obToggleObject created in main to call the methods

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 computer science. Every character is a small letter or a space; the table shows the first four characters, the space, and the last character (the other letters work the same way).

ichtcountres so far
0cC (99 - 32 = 67)1C
1oO2CO
2mM3COM
3pP4COMP
8spacespace (unchanged, so not counted)8COMPUTER
15eE15COMPUTER SCIENCE

The sentence has 15 letters and one space, so count is 15 and res is COMPUTER SCIENCE. The program prints Toggled sentence: COMPUTER SCIENCE and Letters toggled = 15, 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."