Java String Practice Problems with Examples
1. Reverse a String
Write a program to reverse a given string.
Example:
Input: 'hello'
Output: 'olleh'
2. Palindrome Check
Check if a given string is a palindrome (reads the same forwards and backwards).
Example:
Input: 'madam'
Output: true
3. Count Vowels and Consonants
Write a function to count the number of vowels and consonants in a string.
Example:
Input: 'openai'
Output: Vowels = 3, Consonants = 3
4. String to Uppercase
Convert a given string to uppercase without using built-in methods.
Example:
Input: 'java'
Output: 'JAVA'
5. Find Duplicates
Identify duplicate characters in a string and their occurrences.
Example:
Input: 'programming'
Output: g: 2, r: 2, m: 2
6. Anagram Checker
Write a program to check if two strings are anagrams of each other.
Example:
Input: 'listen', 'silent'
Output: true
7. Character Frequency
Count the frequency of each character in a string.
Example:
Input: 'apple'
Output: a: 1, p: 2, l: 1, e: 1
8. Remove Whitespace
Remove all whitespace characters from a string.
Example:
Input: 'a b c'
Output: 'abc'
9. Substring Finder
Find all occurrences of a substring in a given string.
Example:
Input: 'banana', Substring: 'ana'
Output: Found at index 1 and 3
10. String Rotation
Check if one string is a rotation of another string.
Example:
Input: 'abcd', 'dabc'
Output: true
11. Longest Substring Without Repeating Characters
Find the length of the longest substring without repeating characters.
Example:
Input: 'abcabcbb'
Output: 3 (substring: 'abc')
12. String Compression
Compress a string such that 'aaabbc' becomes 'a3b2c1'.
Example:
Input: 'aaabbc'
Output: 'a3b2c1'
13. First Non-Repeating Character
Find the first non-repeating character in a string.
Example:
Input: 'swiss'
Output: 'w'
14. Check for Subsequence
Check if one string is a subsequence of another.
Example:
Input: 'abc', 'aebdc'
Output: true
15. Remove a Character
Remove all occurrences of a specific character from a string.
Example:
Input: 'hello', Character: 'l'
Output: 'heo'
16. Reverse Each Word
Reverse each word in a string without changing their order.
Example:
Input: 'hello world'
Output: 'olleh dlrow'
17. Compare Strings
Compare two strings lexicographically without using built-in methods.
Example:
Input: 'apple', 'banana'
Output: -1 (apple < banana)
18. String Permutations
Generate all permutations of a given string.
Example:
Input: 'abc'
Output: ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
19. Find Common Characters
Find common characters between two strings.
Example:
Input: 'apple', 'ample'
Output: ['a', 'p', 'l', 'e']
20. Case Conversion
Convert uppercase letters to lowercase and vice versa in a string.
Example:
Input: 'HeLLo'
Output: 'hEllO'