Skip to content
Computer ApplicationsICSE Class 10 · String Handling

String Programs in Java: Palindrome, Vowels, Words and Sorting Names (ICSE Class 10)

Updated 17 October 2026 · Fact-checked

String programs loop over the characters with charAt(i) from 0 to length() - 1 and build or test a result: count vowels, reverse the string by looping from the end, count words by counting spaces, capitalise letters after spaces. A string array of names is sorted with bubble or selection sort using compareTo (positive means the left name comes later), and searched with a linear search using equals.

Reading s from the last character to the first builds r; if s.equals(r) is true, the string is a palindrome.

Illustration drawn for QuestionBuddy.

Understand String Programs: Palindrome, Words and Sorting Names

The basic loop.

for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    // test or use c
}

Use Character.isLetter, isDigit, toUpperCase and the like on c.

Counting vowels. Test with "aeiou".indexOf(Character.toLowerCase(c)) >= 0; the position is -1 if c is not a vowel.

Reversing and palindrome. Start with String r = ""; and loop i from s.length() - 1 down to 0 adding r = r + s.charAt(i);. A string is a palindrome if it equals its reverse: s.equals(r). Use equalsIgnoreCase if case should be ignored ("Radar").

Counting words. For a sentence with single spaces between words, the number of words is the number of spaces plus 1. Use trim() first to remove stray spaces at the ends.

Changing the string. Strings are not changed in place; build a new string with r = r + c; (or r += c).

Sorting a String array. Same loops as for numbers, but compare with compareTo: for ascending order, swap when a[j].compareTo(a[j + 1]) > 0. Capital letters come before small letters in ASCII, so use compareToIgnoreCase if the names have mixed case.

if (a[j].compareTo(a[j + 1]) > 0) {
    String t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}

Linear search in a String array. Loop over the array and test a[i].equals(key); never use == for contents.

Selection sort and bubble sort are required for strings; binary search of strings is not asked, only linear search.

String Programs: Palindrome, Words and Sorting Names: key facts

Loop over a string
for i from 0 to s.length() - 1 | c = s.charAt(i)
Indices start at 0.
Palindrome test
Build r by looping from the last index to 0 | Palindrome if s.equals(r)
equalsIgnoreCase ignores case.
Words in a sentence
Words = number of spaces + 1 | Trim the sentence first
Valid for single spaces between words.
Sorting names
Ascending: swap when a[j].compareTo(a[j + 1]) is greater than 0 | Descending: when less than 0
Capital letters sort before small letters.

How to answer String Programs: Palindrome, Words and Sorting Names questions

Use this routine for string program questions.

  1. 1Write the string with indices; decide whether to loop forwards or backwards.
  2. 2Initialise the counter (0) or the result string (empty) before the loop.
  3. 3Inside the loop test or transform each charAt(i) and update the counter or result.
  4. 4After the loop, print the result with the labels the question asks for.
  5. 5For arrays of strings, use compareTo for order and equals for equality.

String Programs: Palindrome, Words and Sorting Names in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Alphabetical class register

Suppose a teacher has four names on slips and arranges them in alphabetical order by repeatedly swapping neighbouring slips that are in the wrong order.

Which method decides that "Mohan" must come after "Anil"?

Try it first, then show the solutionSolution
  1. "Mohan".compareTo("Anil") is 77 - 65 = 12, a positive number.
  2. A positive result means Mohan comes after Anil, so the pair is swapped only when Mohan is on the left.

Answer: compareTo; its positive value shows the left name is later.

Memory hook: Positive means swap.

Tricks to remember String Programs: Palindrome, Words and Sorting Names

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Backwards loop reverses

    Looping from length - 1 down to 0 and adding each char builds the reverse.

    Use it when: Reverse and palindrome.

  • Spaces plus one

    Words = spaces + 1, just as 2 cuts make 3 pieces of string.

    Use it when: Counting words.

  • equals for contents

    Compare contents with equals or compareTo, never with ==.

    Use it when: Searching strings.

String Programs: Palindrome, Words and Sorting Names: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Counting vowels · 2 marks · Short answerBoard level

Write the output:

String s = "Education";
int v = 0;
for (int i = 0; i < s.length(); i++) {
    char c = Character.toLowerCase(s.charAt(i));
    if ("aeiou".indexOf(c) >= 0)
        v++;
}
System.out.println(v);
Try it first, then show the solutionSolution
  1. The letters are E, d, u, c, a, t, i, o, n; after lower-casing the vowels are e, u, a, i, o. (1 mark)
  2. The counter v ends at 5, so 5 is printed. (1 mark)

Answer: 5

Example 2: Reverse and palindrome · 3 marks · Short answerHarder board level

Write the output:

String s = "Radar";
String r = "";
for (int i = s.length() - 1; i >= 0; i--)
    r = r + s.charAt(i);
System.out.println(r + " " + s.equals(r) + " " + s.equalsIgnoreCase(r));
Try it first, then show the solutionSolution
  1. Reading Radar from the end gives r, a, d, a, R: r = "radaR". (1 mark)
  2. s.equals(r) compares "Radar" and "radaR": the first letters differ in case, so false. (1 mark)
  3. equalsIgnoreCase ignores case, so true. Output: radaR false true. (1 mark)

Answer: radaR false true

Example 3: Sorting names · 4 marks · Short answerHarder board level

Write the output:

String[] n = {"Mohan", "Anil", "Zoya", "Dev"};
for (int i = 0; i < n.length - 1; i++)
    for (int j = 0; j < n.length - 1 - i; j++)
        if (n[j].compareTo(n[j + 1]) > 0) {
            String t = n[j]; n[j] = n[j + 1]; n[j + 1] = t;
        }
for (int i = 0; i < n.length; i++)
    System.out.print(n[i] + " ");
Try it first, then show the solutionSolution
  1. Pass 1: (Mohan, Anil) swap, (Mohan, Zoya) no swap, (Zoya, Dev) swap: Anil Mohan Dev Zoya. (1 mark)
  2. Pass 2: (Anil, Mohan) no, (Mohan, Dev) swap: Anil Dev Mohan Zoya. (1 mark)
  3. Pass 3: (Anil, Dev) no swap; the array is now in ascending alphabetical order. (1 mark)
  4. The names are printed with a space after each: Anil Dev Mohan Zoya. (1 mark)

Answer: Anil Dev Mohan Zoya

Example 4: Case study: words and capital letters · 4 marks · Case-basedHOTS / competency

A sentence is processed:

String s = "the quick fox";
int w = 1;
String r = "";
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == ' ')
        w++;
    if (i == 0 || s.charAt(i - 1) == ' ')
        r += Character.toUpperCase(c);
    else
        r += c;
}
System.out.println(r + " " + w);

(i) What does the variable w count? (1)
(ii) What is the output? (2)
(iii) What does the condition i == 0 || s.charAt(i - 1) == ' ' select? (1)

Try it first, then show the solutionSolution
  1. (i) w starts at 1 and increases for every space: it counts the words.
  2. (ii) Spaces at indices 3 and 9 make w = 3. The first letter of each word (the characters at index 0 and after each space) becomes upper case: The Quick Fox. Output: The Quick Fox 3
  3. (iii) It selects the first letter of every word: index 0, or a character that follows a space.

Answer: (i) number of words (ii) The Quick Fox 3 (iii) the first letter of each word

Example 5: Linear search in a String array · 3 marks · Short answerHarder board level

Write the output:

String[] a = {"red", "blue", "green"};
String[] keys = {"blue", "pink"};
for (int k = 0; k < keys.length; k++) {
    int pos = -1;
    for (int i = 0; i < a.length; i++)
        if (a[i].equals(keys[k])) { pos = i; break; }
    System.out.print(pos + " ");
}
Try it first, then show the solutionSolution
  1. For "blue": a[0] red no, a[1] blue yes, so pos = 1. (1 mark)
  2. For "pink": no element matches, so pos stays -1. (1 mark)
  3. The positions are printed with a space after each: 1 -1. (1 mark)

Answer: 1 -1

Where students lose marks in String Programs: Palindrome, Words and Sorting Names

The slips examiners see most often, and what to write instead.

  • Mistake: Using == to compare strings

    == compares references, not contents.

    Wrong working

    if (a[i] == key)

    Right working

    if (a[i].equals(key))

    Do this: Use equals or compareTo.

    Usually costs: 1 mark

  • Mistake: Looping to length() instead of length() - 1

    charAt(length()) is outside the string and causes StringIndexOutOfBoundsException.

    Wrong working

    for (i = 0; i <= s.length(); i++)

    Right working

    for (i = 0; i < s.length(); i++)

    Do this: Use < length().

    Usually costs: 1 mark

  • Mistake: Ignoring case in a palindrome test

    equals treats R and r as different.

    Wrong working

    "Radar" is not a palindrome with equals

    Right working

    Use equalsIgnoreCase when case should not matter

    Do this: Read the question for case rules.

    Usually costs: 1 mark

Exam tips

  • In string programs, state the logic in one line before the code; it gives method marks.
  • Use trim() when counting words from user input.
  • For sorting strings, name compareTo explicitly.
  • Print the result with the label the question uses.

Practice questions on String Programs: Palindrome, Words and Sorting Names

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    What does "abc".compareTo("abd") return?
    (a) 1 (b) -1 (c) 0 (d) true

    Hint

    First differing characters: c and d.

    Answer

    (b) -1, since 'c' minus 'd' is -1.

  2. Q2 · 1 mark · Very short answerBoard level

    Write the expression that gives the index of the last character of a String s.

    Hint

    Length minus one.

    Answer

    s.length() - 1

  3. Q3 · 2 marks · Short answerHarder board level

    Write the output:

    String s = "AbC";
    String r = "";
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isUpperCase(c))
            r += Character.toLowerCase(c);
        else
            r += Character.toUpperCase(c);
    }
    System.out.println(r);
    Hint

    Toggle each letter.

    Answer

    aBc

  4. Q4 · 4 marks · Short answerHarder board level

    Write a program fragment that counts the words in a sentence s whose words are separated by single spaces, and give the count for "to be or not".

    Hint

    Count the spaces.

    Answer

    int w = 1;
    for (int i = 0; i < s.length(); i++)
    if (s.charAt(i) == ' ')
    w++;
    System.out.println(w);
    For "to be or not" there are 3 spaces, so w = 4.

  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): A string is a palindrome if it equals its reverse.
    Reason (R): A palindrome reads the same from left to right and from right to left.
    (a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.

    Hint

    Does R define the term?

    Answer

    (a) Both are true and R explains A.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.How do you reverse a string with a loop?Show answer

    Loop from the last index down to 0 and add each char to a new string.

  2. 2.How many words in a sentence with 4 spaces?Show answer

    5 (single spaces).

  3. 3.Which method sorts order for strings?Show answer

    compareTo.

  4. 4.What does a positive compareTo result mean?Show answer

    The left string comes after the right string.

  5. 5.Which search is asked for string arrays?Show answer

    Linear search.

  6. 6.What is a palindrome?Show answer

    A string that reads the same forwards and backwards.

String Programs: Palindrome, Words and Sorting Names: frequently asked questions

Why not use == for strings?

== tests whether two variables refer to the same object; equals tests whether the contents are the same.

Can a char be added to a String with +=?

Yes. r += c appends the character c to the String r.

How do I sort names ignoring case?

Use compareToIgnoreCase in place of compareTo.