CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

String Programs: Palindrome, Reverse, Extraction and Counting

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn the board string programs in Java: charAt(), reversing a word, checking a palindrome, and counting vowels and letters. A string's index runs from 0 to length() - 1, and charAt(i) pulls out one character.

Follows the syllabus of: ICSE Class 10 Computer Applications

On screen in this lesson

Index positions in "INDIA"

IndexcharAt(index)
0'I'
1'N'
2'D'
3'I'
4'A'

Two methods you need

length() gives the number of characters
charAt(i) gives the character at index i
Last index is length() - 1
charAt(length()) throws an exception

Trace: reversing "ABC"

icharAt(i)r after
startnone""
0'A'"A"
1'B'"BA"
2'C'"CBA"

Palindrome: exam details

Print a message with if-else, not just true
Madam vs madam: use equalsIgnoreCase()
Or change case first with toUpperCase()
Test: MADAM, LEVEL, and a non-palindrome like JAVA

Board exam string programs

Palindrome word, and reverse of a word
Count vowels, consonants, spaces or digits
Frequency of each letter in a sentence
Initials of a name, or first letter of each word
Change case or replace a character

Mistakes that lose marks

Using <= length() instead of < length()
Comparing strings with == instead of equals()
Forgetting to start the counter at 0 or 1
Mixing 'A' (a char) with "A" (a String)

Quick answers

Why does charAt(5) crash on INDIA?

The index runs 0 to 4, so index 5 does not exist.

Why should you not compare strings with ==?

== checks whether they are the same object, not the same text. Use equals().

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. MADAM reads the same forwards and backwards. How can a Java program check that? Almost every board paper has a string program of this kind. Today we will extract characters, reverse a string, check a palindrome, and count vowels, words and letters, one loop at a time.

A string is a sequence of characters, and each character has a position number, called its index. The index always starts at zero, so in INDIA, index zero is I. Index one is N. Index two is D. Index three is I again. Index four is A, the last one.

Two methods do most of the work. The length method gives the number of characters, so INDIA has length five. The charAt method gives back the single character, a char, at a given index. So the last index is always length minus one, which is four here. Asking for charAt five throws a StringIndexOutOfBoundsException.

This loop is the heart of every string program. The variable w holds the word JAVA. The loop variable i goes from zero, while i is less than the length. Notice, less than, not less than or equal to. Each round, charAt of i pulls out one character, and prints it on its own line.

To reverse a string, we build a new string r, which starts empty. The trick is in one line. Each new character is joined in front of r, not behind it. So the first letter ends up last, and the last letter ends up first. The output is K C I W K.

Let us trace it with a short word, ABC. At the start, r is empty. When i is zero, A goes in front, so r is A. When i is one, B goes in front, so r is B A. When i is two, C goes in front, so r is C B A. You can also loop backwards from length minus one down to zero, and join each character at the end.

A palindrome is a word that reads the same backwards, like MADAM or LEVEL. So we reverse the word, and compare it with the original. The equals method compares the characters of two strings, and gives true here. Never use double equals to compare strings, because it checks whether they are the same object, not the same text.

In the exam, a few details earn full marks. Use if else to print a clear message, like Palindrome or Not a palindrome. Capital M and small m are different characters, so use equalsIgnoreCase to ignore case. Another way is to change the word to capitals first, with toUpperCase. And test with a palindrome, and with a word that is not one, like JAVA.

Now counting. The variable t holds EDUCATION, and c is the counter. Each round, ch is one character. The indexOf method searches for ch inside the string A E I O U. It gives minus one when the character is not found. So if the answer is zero or more, ch is a vowel, and c goes up. EDUCATION has five vowels.

To count words, we count the spaces between them. Two words have one space, so the counter starts at one. Here we compare a char with a space in single quotes, and double equals is correct for chars. I love Java has two spaces, so it prints three words. This works when words are separated by single spaces, so use trim first to remove extra spaces at the ends.

Frequency means how many times something appears. Pause and predict. How many times does A appear in BANANA? The loop checks each character, and adds one to c for every A. The answer is three. For the frequency of every letter, put this inside another loop that goes from A to Z.

Here is a favourite board program. Print the first letter of each word in a name. The trick is a space added at the front of the name. Now every word starts right after a space. When charAt of i is a space, we print the next character, at i plus one. The loop stops one early, so i plus one never goes past the end. The output is R G S.

These are the string programs board papers ask again and again. Checking a palindrome word, and printing the reverse. Counting vowels, consonants, spaces or digits, with Character methods like isDigit and isLetter. Finding the frequency of each letter in a sentence. Printing the initials of a name, or the first letter of each word. And changing the case, or replacing one character with another.

Avoid four common mistakes. Writing less than or equal to length goes one step too far, and throws an exception. Comparing strings with double equals instead of equals can give false for the same text. Starting a counter at the wrong value, zero or one, gives an answer that is off by one. And mixing single quotes and double quotes, because A in single quotes is a char, but in double quotes it is a String.

Let us revise. A string's index runs from zero to length minus one. charAt of i extracts one character at a time. To reverse, join each character in front of r, and for a palindrome, compare with equals. To count vowels, words or letters, check each character with an if, and add one to a counter. Practise each program with your own name and words.

Courses that teach this

CourseUnit
ICSE Class 10 Computer ApplicationsString Handling

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →