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

ICSE Java String Methods Guide

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)
9 views4 pages

ICSE Java String Methods Guide

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

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 [Link](), 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 [Link]() or isDigit()

Page 3
ICSE Java String Programs - Revision Worksheet

Example:

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

Page 4

Common questions

Powered by AI

To change "CISE" to "ICSE", apply "replace('C', 'I')", resulting in "IISE", and then "replaceFirst('I', 'C')" to get "ICSE". This shows "replace()" can substitute all occurrences of a character, while carefully stacking multiple replacements or using "replaceFirst()" helps in detailed value transformations .

The "equals()" method checks if two strings are exactly the same, considering case sensitivity. Therefore, "Java".equals("java") returns false because of case differences . In contrast, "equalsIgnoreCase()" ignores case, so "Java".equalsIgnoreCase("java") returns true, treating both strings as equal without considering letter casing .

The program uses checks for letters, digits, and special characters. It treats vowels as 'a, e, i, o, u' using conditions or a set. 'HeLlo123!' contains letters, digits, and a special character. Iterating over each character: 'H' and 'e' are letters, 'e' is a vowel; 'L', 'l', and 'o' are letters, 'o' is a vowel; '1', '2', '3' are digits; '!' is a special character. Counts: Vowels = 2 ('e', 'o'), Consonants = 3 ('H', 'L', 'l'), Digits = 3 ('1', '2', '3'), Special Characters = 1 ('!').

Palindrome checking through string reversal leverages symmetry, where a string reads the same forwards and backwards. Mathematically, it represents a one-to-one positional correspondence across the string's midpoint, akin to reflective symmetry in geometry. Reversal and comparison ensure equivalence in sequences, foundational to recognizing such symmetrical patterns .

The "length()" method returns the number of characters in a string. For "hello", it returns 5 . The "charAt(i)" method returns the character at the specified index 'i'. To print each character of "hello" along with its index, you could iterate over the string using a for loop up to the "length()" of the string and use "charAt(i)" to get each character. For example: ``` for(int i = 0; i < "hello".length(); i++) { System.out.println("Index " + i + ": " + "hello".charAt(i)); } ``` This loop prints: Index 0: h Index 1: e Index 2: l Index 3: l Index 4: o.

Use Java's "Character.isLetter()" and "Character.isDigit()" within a loop to validate string content. For "Java123": - Iterate through each character. - For 'J', 'a', 'v', 'a', "isLetter()" returns true. - For '1', '2', '3', "isDigit()" returns true. The mixed results indicate both letters and digits. This combination approach confirms non-exclusive character types in strings .

Use the "split(" ")" method in Java to divide the string by spaces into words: "I enjoy learning Java" splits into ["I", "enjoy", "learning", "Java"]. - First word: The word at index 0, "I". - Middle word: Typically, index 1 in a 4-word sentence, "enjoy". - Last word: The final word in the array, "Java". This process effectively isolates specific word positions within any given sentence .

The "split()" method separates a string by specified delimiters. For "Java is fun", use "split(" ")" by spaces, resulting in ["Java", "is", "fun"], which contains three words. This method divides text into manageable parts, counting array elements yields total words, assisting in frequency calculations .

The "substring(i, j)" method extracts the part of a string from index 'i' to 'j-1'. For "programming": - "programming".substring(0, 7) returns "program". - "programming".substring(7, 11) returns "ming". - "programming".substring(3, 9) returns "grammi". Thus, different indices can be chosen to extract specific segments of the string as needed .

To reverse a string and check for palindrome: 1. Initialize two pointers, one at the start and another at the end of "racecar". 2. Swap characters at these pointers as you move the start pointer forward and the end pointer backward. 3. Construct a reversed string. 4. Compare the reversed string to the original. 5. If equal, the string is a palindrome. For "racecar", after reversing, you get "racecar", which equals the original, confirming it's a palindrome .

You might also like