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 examplea(97) becomesA(65). - A capital (
ch >= 'A' && ch <= 'Z') becomes a small letter by(char)(ch + 32). For exampleZ(90) becomesz(122). - Any other character is copied unchanged.
Hello World 123, ISC-2026!becomeshELLO wORLD 123, isc-2026!.
The cast (char) is needed because ch - 32 is an int.
Algorithm
- Read the sentence into
str. If it is empty, printINVALID INPUTand stop. - Set
res = ""andcount = 0. - For every character
chofstr:- if
chis between'a'and'z'(a small letter), add(char) (ch - 32)toresand add 1 tocount; - otherwise, if
chis between'A'and'Z'(a capital), add(char) (ch + 32)toresand add 1 tocount; - otherwise (digit, space or punctuation) add
chtoresunchanged.
- if
- Print the toggled sentence
resand 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
str | String | The sentence entered by the user |
res | String | The sentence after toggling, built one character at a time |
count | int | Number of letters toggled |
i | int | Loop counter over the characters of str |
ch | char | The 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 = 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 aZ@[{ 9 zA. This input is chosen because@,[, ` and{` have ASCII codes just outside the letter ranges, so they must not change.
i | ch | Rule used | Added to res | count |
|---|---|---|---|---|
| 0 | a | small letter, 97 - 32 = 65 | A | 1 |
| 1 | Z | capital, 90 + 32 = 122 | z | 2 |
| 2 | @ | not a letter | @ | 2 |
| 3 | [ | not a letter | [ | 2 |
| 4 | ` | not a letter | ` | 2 |
| 5 | { | not a letter | { | 2 |
| 6 | space | not a letter | space | 2 |
| 7 | 9 | not a letter | 9 | 2 |
| 8 | space | not a letter | space | 2 |
| 9 | z | small letter, 122 - 32 = 90 | Z | 3 |
| 10 | A | capital, 65 + 32 = 97 | a | 4 |
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
- 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."