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
mainreads the line withnextLine()intosand createsobwithnew Toggle(s). The parameterised constructor storessinstr, setsresto empty andcountto 0.isValid()returnstrueifstris not empty. OtherwisemainprintsINVALID INPUT.toggleChar(ch)returns(char)(ch - 32)for a small letter,(char)(ch + 32)for a capital, andchitself for any other character.toggle()visits every positioniofstr:ch = str.charAt(i)andt = toggleChar(ch).- If
tdiffers fromch, the character was a letter, socountincreases. tis added tores.
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
| Variable | Data Type | Purpose |
|---|---|---|
str | String | Data member: the sentence entered by the user |
res | String | Data member: the sentence after toggling |
count | int | Data member: number of letters toggled |
s | String | Parameter of the constructor, and the variable in main that holds the typed sentence |
ch | char | Parameter of toggleChar(): the character to change; also the character of str being examined in toggle() |
t | char | The toggled form of ch returned by toggleChar() |
i | int | Loop counter: position in str |
sc | Scanner | Reads the line from the keyboard in main |
args | String[] | Command line arguments of main (not used) |
ob | Toggle | Object 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 = 13Sample 2
Enter a sentence: aZ@[`{ 9 zA
Toggled sentence: Az@[`{ 9 Za
Letters toggled = 4Sample 3
Enter a sentence: computer science
Toggled sentence: COMPUTER SCIENCE
Letters toggled = 15Dry 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).
i | ch | t | count | res so far |
|---|---|---|---|---|
| 0 | c | C (99 - 32 = 67) | 1 | C |
| 1 | o | O | 2 | CO |
| 2 | m | M | 3 | COM |
| 3 | p | P | 4 | COMP |
| 8 | space | space (unchanged, so not counted) | 8 | COMPUTER |
| 15 | e | E | 15 | COMPUTER 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
- Count the cases "Write a program to count the uppercase and lowercase letters of a sentence."
- Capitalise each word "Write a program to change the first letter of every word of a sentence to a capital."
- Next letter "Write a program to replace every letter of a sentence by the next letter of the alphabet."