Solved ProgramsICSEClass 10Computer Applications

10 Most Important String Programs for ICSE Class 10 (Solved with Output)

The 10 string programs that appear most often in ICSE Class 10 Computer Applications exams: palindrome, vowel count, word reversal, piglatin, character frequency and more, each fully solved in Java with sample output.

16 min read
10 Most Important String Programs for ICSE Class 10 (Solved with Output)

Almost every ICSE Computer Applications paper has at least one string program in Section B. The good news is that the same patterns repeat year after year. If you master these 10 programs, you can handle almost any string question in the board exam.

String Methods You Must Know

Java String methods cheat sheet with examples and results
  • length(): number of characters
  • charAt(i): the character at index i
  • indexOf(ch) and lastIndexOf(ch): position of a character, or -1 if it is not found
  • substring(a, b): characters from index a to b − 1
  • toUpperCase() and toLowerCase()
  • equals(), equalsIgnoreCase() and compareTo()
  • trim(), replace(a, b), startsWith(), endsWith() and concat()
  • Character methods: Character.isLetter(), isDigit(), isUpperCase(), isWhitespace() and toUpperCase()

Program 1 – Check a Palindrome String

A palindrome reads the same forwards and backwards, for example MADAM or LEVEL.

import java.util.Scanner;

class PalindromeString {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String s = sc.next().toUpperCase();
        String rev = "";
        for (int i = s.length() - 1; i >= 0; i--)
            rev = rev + s.charAt(i);

        if (s.equals(rev))
            System.out.println(s + " is a palindrome");
        else
            System.out.println(s + " is not a palindrome");
    }
}

Output: Enter a word: Level → LEVEL is a palindrome

Program 2 – Count Vowels, Consonants, Digits and Spaces

import java.util.Scanner;

class CountChars {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine().toUpperCase();
        int v = 0, c = 0, d = 0, sp = 0;

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if ("AEIOU".indexOf(ch) != -1)
                v++;
            else if (Character.isLetter(ch))
                c++;
            else if (Character.isDigit(ch))
                d++;
            else if (ch == ' ')
                sp++;
        }
        System.out.println("Vowels: " + v + ", Consonants: " + c
                           + ", Digits: " + d + ", Spaces: " + sp);
    }
}

Output: for "ICSE Exam 2027" the result is Vowels: 4, Consonants: 4, Digits: 4, Spaces: 2

Program 3 – Reverse Each Word of a Sentence

Character indices of the string JAVA IS FUN

Input: JAVA IS FUN gives the output AVAJ SI NUF

import java.util.Scanner;

class ReverseWords {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine() + " ";
        String word = "", result = "";

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch != ' ')
                word = ch + word;      // adding at the front reverses the word
            else {
                result = result + word + " ";
                word = "";
            }
        }
        System.out.println(result.trim());
    }
}

Program 4 – Piglatin Conversion

To convert a word to Piglatin, move every letter before the first vowel to the end of the word and add "AY". For example, TROUBLE becomes OUBLETRAY. A word that begins with a vowel, such as APPLE, simply gets "AY" added at the end, giving APPLEAY.

import java.util.Scanner;

class Piglatin {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String s = sc.next().toUpperCase();
        int pos = -1;

        for (int i = 0; i < s.length(); i++) {
            if ("AEIOU".indexOf(s.charAt(i)) != -1) {
                pos = i;
                break;
            }
        }

        if (pos == -1)
            System.out.println(s + "AY");     // no vowel in the word
        else
            System.out.println(s.substring(pos) + s.substring(0, pos) + "AY");
    }
}

Program 5 – Frequency of Each Alphabet

import java.util.Scanner;

class Frequency {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine().toUpperCase();

        System.out.println("Letter\tFrequency");
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == ch)
                    count++;
            }
            if (count > 0)
                System.out.println(ch + "\t" + count);
        }
    }
}

Program 6 – Print the Initials of a Name

Input: Mohandas Karamchand Gandhi gives the output M.K. Gandhi

import java.util.Scanner;

class Initials {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter full name: ");
        String s = sc.nextLine().trim();
        int last = s.lastIndexOf(' ');
        String result = "" + s.charAt(0) + ".";

        for (int i = 0; i < last; i++) {
            if (s.charAt(i) == ' ')
                result = result + s.charAt(i + 1) + ".";
        }
        result = result + " " + s.substring(last + 1);
        System.out.println(result);
    }
}
Note: this program assumes the name has at least two words separated by single spaces.

Program 7 – Longest Word in a Sentence

import java.util.Scanner;

class LongestWord {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine() + " ";
        String word = "", longest = "";

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch != ' ')
                word = word + ch;
            else {
                if (word.length() > longest.length())
                    longest = word;
                word = "";
            }
        }
        System.out.println("Longest word: " + longest
                           + " (" + longest.length() + " letters)");
    }
}

Program 8 – Toggle the Case of Each Character

Input: HeLLo World gives the output hEllO wORLD

import java.util.Scanner;

class ToggleCase {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine();
        String result = "";

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isUpperCase(ch))
                result += Character.toLowerCase(ch);
            else if (Character.isLowerCase(ch))
                result += Character.toUpperCase(ch);
            else
                result += ch;
        }
        System.out.println(result);
    }
}

Program 9 – Count the Words that Start with a Vowel

import java.util.Scanner;

class VowelWords {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = " " + sc.nextLine().toUpperCase();
        int count = 0;

        for (int i = 0; i < s.length() - 1; i++) {
            if (s.charAt(i) == ' ' && "AEIOU".indexOf(s.charAt(i + 1)) != -1)
                count++;
        }
        System.out.println("Words starting with a vowel: " + count);
    }
}

Output: for "An apple is on the table" the result is 4 (An, apple, is, on).

Program 10 – Sort Names Alphabetically (String Array)

import java.util.Scanner;

class SortNames {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String names[] = new String[5];
        System.out.println("Enter 5 names:");
        for (int i = 0; i < 5; i++)
            names[i] = sc.nextLine();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4 - i; j++) {
                if (names[j].compareToIgnoreCase(names[j + 1]) > 0) {
                    String t = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = t;
                }
            }
        }

        System.out.println("Sorted names:");
        for (int i = 0; i < 5; i++)
            System.out.println(names[i]);
    }
}

Common Mistakes to Avoid

  • Using == to compare strings. Always use equals() or equalsIgnoreCase().
  • Forgetting to add a space at the end of the sentence (s + " ") when you extract words character by character. Without it, the last word is never processed.
  • Mixing up next() (reads one word) and nextLine() (reads the whole line).
  • Calling nextLine() straight after nextInt() without clearing the leftover newline. Add an extra sc.nextLine(); in between.

Practise each program, then try changing it slightly, for example counting words that end with a vowel instead. Examiners love small variations like this.