Java String Practice Questions with Examples
1. Reverse a String
Example 1: Input: hello -> Output: olleh
Example 2: Input: Vinay -> Output: yaniV
Example 3: Input: Java -> Output: avaJ
2. Check if a String is Palindrome
Example 1: Input: madam -> Output: true
Example 2: Input: hello -> Output: false
Example 3: Input: racecar -> Output: true
3. Count Vowels and Consonants
Example 1: Input: Vinay -> Output: Vowels: 2, Consonants: 3
Example 2: Input: Education -> Output: Vowels: 5, Consonants: 4
Example 3: Input: xyz -> Output: Vowels: 0, Consonants: 3
4. Check if Two Strings Are Anagrams
Example 1: Input: listen, silent -> Output: true
Example 2: Input: hello, world -> Output: false
Example 3: Input: evil, vile -> Output: true
5. Find First Non-Repeating Character
Example 1: Input: aabbccde -> Output: d
Example 2: Input: xxyyzz -> Output: No unique character
Example 3: Input: programming -> Output: p
6. Remove All Duplicate Characters
Example 1: Input: hello -> Output: helo
Example 2: Input: aabbcc -> Output: abc
Example 3: Input: banana -> Output: ban
7. Check if a String Contains Only Digits
Example 1: Input: 12345 -> Output: true
Example 2: Input: 12a34 -> Output: false
Example 3: Input: 000 -> Output: true
8. Find Most Frequent Character
Example 1: Input: hello -> Output: l
Example 2: Input: banana -> Output: a
Example 3: Input: aabbccc -> Output: c
9. Print All Permutations of a String
Example 1: Input: abc -> Output: abc, acb, bac, bca, cab, cba
Example 2: Input: ab -> Output: ab, ba
Example 3: Input: a -> Output: a
10. Convert First Letter of Each Word to Uppercase
Example 1: Input: hello world -> Output: Hello World
Example 2: Input: vinay kumar -> Output: Vinay Kumar
Example 3: Input: java programming -> Output: Java Programming
11. Count the number of words in a sentence
Example 1: Input: This is Java -> Output: 3
Example 2: Input: Hello World -> Output: 2
Example 3: Input: Java -> Output: 1
12. Find the longest word in a sentence
Example 1: Input: I love Java -> Output: Java
Example 2: Input: Coding is fun -> Output: Coding
Example 3: Input: Hi Vinay -> Output: Vinay
13. Remove all whitespace from a String
Example 1: Input: a b c -> Output: abc
Example 2: Input: Vinay Kumar -> Output: VinayKumar
Example 3: Input: Java -> Output: Java
14. Swap two Strings without using a third variable
Example 1: Input: abc, xyz -> Output: xyz, abc
Example 2: Input: Vinay, Aarya -> Output: Aarya, Vinay
Example 3: Input: Java, Code -> Output: Code, Java
15. Check valid palindrome ignoring cases and symbols
Example 1: Input: A man, a plan, a canal: Panama -> Output: true
Example 2: Input: race a car -> Output: false
Example 3: Input: No lemon, no melon -> Output: true
16. Length of longest substring without repeating characters
Example 1: Input: abcabcbb -> Output: 3
Example 2: Input: bbbbb -> Output: 1
Example 3: Input: pwwkew -> Output: 3
17. Replace all spaces in string with '%20'
Example 1: Input: Mr John Smith -> Output: Mr%20John%20Smith
Example 2: Input: a b c -> Output: a%20b%20c
Example 3: Input: Java Code -> Output: Java%20Code
18. Check if two strings are equal without equals()
Example 1: Input: Java, Java -> Output: true
Example 2: Input: abc, xyz -> Output: false
Example 3: Input: Hello, hello -> Output: false
19. Check if one string is a rotation of another
Example 1: Input: abcde, deabc -> Output: true
Example 2: Input: waterbottle, erbottlewat -> Output: true
Example 3: Input: abc, cabc -> Output: false
20. Count how many times a character appears
Example 1: Input: banana, a -> Output: 3
Example 2: Input: hello, l -> Output: 2
Example 3: Input: mississippi, s -> Output: 4