Java String Practice Questions (Infosys Level)
1. Reverse a String
Problem: Reverse a string manually.
Example: "hello" -> "olleh"
Logic 1 (Loop and charAt):
Loop from the end using charAt(i) and build a new string.
Logic 2 (Recursive using substring):
Use substring and recursion to reverse the string.
2. Check Palindrome
Problem: Check if a string is the same forwards and backwards.
Example: "level" -> true
Logic 1: Compare start and end characters using charAt().
Logic 2: Reverse the string and compare using equals().
3. Remove Duplicate Characters
Problem: Remove repeated characters.
Example: "banana" -> "ban"
Logic 1: Use indexOf() to check if the character already exists in result.
Logic 2: Use nested loops to manually check duplicates.
4. Character Frequency
Problem: Count how many times each character appears.
Example: "apple" -> a=1, p=2, l=1, e=1
Logic 1: Use int array[256] and charAt() for ASCII mapping.
Logic 2: Use nested loops to count frequency manually.
5. Remove Vowels
Problem: Eliminate all vowels.
Example: "education" -> "dctn"
Logic 1: Use multiple replace() calls for vowels.
Logic 2: Use charAt() and check if the character is a vowel.
Java String Practice Questions (Infosys Level)
6. Capitalize First Letter of Each Word
Problem: Convert first letter of each word to uppercase.
Example: "hello world" -> "Hello World"
Logic 1: Use split() and substring() to capitalize each word.
Logic 2: Traverse with charAt() and capitalize after space.
7. Check Anagram
Problem: Check if two strings are made up of same characters.
Example: "listen" and "silent" -> true
Logic 1: Remove characters of first string from the second.
Logic 2: Sort both strings manually and compare.
8. Count Words
Problem: Count total words in a sentence.
Example: "This is Java" -> 3
Logic 1: Use split(" ") and count length.
Logic 2: Count spaces in string and add 1.
9. Replace Digits with '*'
Problem: Replace all digits with '*'
Example: "a1b2" -> "a*b*"
Logic 1: Use replaceAll("[0-9]", "*").
Logic 2: Loop and check if character is digit.
10. Count Substring Occurrences
Problem: Count how many times a substring appears.
Example: "ababcab", "ab" -> 3
Logic 1: Use indexOf in a loop and update start index.
Logic 2: Use split(substring) and subtract 1 from length.