0% found this document useful (0 votes)
2 views4 pages

Java_String_Revision_Worksheet

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Java_String_Revision_Worksheet

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ICSE Java String Programs - Revision Worksheet

1. Basic String Methods

- length(): Returns number of characters.

Example: "hello".length() -> 5

- charAt(i): Returns character at index i.

Example: "hello".charAt(1) -> 'e'

- substring(i, j): Returns part from index i to j-1.

Example: "hello".substring(1,4) -> "ell"

- indexOf('x'): Returns index of first occurrence of character.

Example: "apple".indexOf('p') -> 1

- equals(): Checks if two strings are equal (case-sensitive).

Example: "hello".equals("Hello") -> false

- equalsIgnoreCase(): Ignores case while comparing.

Example: "hello".equalsIgnoreCase("HELLO") -> true

- toUpperCase() / toLowerCase(): Converts string case.

Example: "Hi".toUpperCase() -> "HI"

- replace('a','b'): Replaces all 'a' with 'b'.

Example: "java".replace('a','o') -> "jovo"

2. Count Vowels, Consonants, Digits, Special Characters

Program:

Input a string. Count and display the number of vowels, consonants, digits and special characters.

Page 1
ICSE Java String Programs - Revision Worksheet

Logic:

- Use Character.isLetter(), isDigit()

- Check vowels using a set or condition (a,e,i,o,u)

Example:

Input: "HeLlo123!"

Vowels: 2, Consonants: 3, Digits: 3, Special: 1

3. Reverse and Palindrome

Program 1: Reverse a string

Logic: Traverse string from end to start.

Program 2: Check Palindrome

Logic: If original string equals reversed string.

Example:

Input: "madam" -> Palindrome

Input: "hello" -> Not Palindrome

4. Word and Character Frequency

Program: Count number of words and frequency of a given character.

Logic:

- Words: Use split(" ") or count spaces + 1

- Frequency: Loop through characters and count matches

Example:

Page 2
ICSE Java String Programs - Revision Worksheet

Input: "Java is easy", character: 'a' -> Frequency: 2, Words: 3

5. Extract Parts of String

Program: Extract first, last or middle word.

Logic:

- Use split(" ") and index values.

Example:

Input: "I love Java" -> First: I, Middle: love, Last: Java

6. Modify and Transform String

Programs:

- Convert lowercase to uppercase (use toUpperCase())

- Replace a word (use replaceAll() or replace())

Example:

Input: "Hello world", replace 'world' with 'Java' -> Output: "Hello Java"

7. Compare and Validate

Programs:

- Compare strings using equals(), compareTo()

- Validate if string contains only letters/digits

Logic:

- Loop through characters and check Character.isLetter() or isDigit()

Page 3
ICSE Java String Programs - Revision Worksheet

Example:

Input: "Test123" -> Contains digits and letters.

Page 4

You might also like