String Programs
String manipulation problems are among the most frequently asked in interviews, and practicing them builds comfort with Java's built-in String methods.
Why practice these?
String manipulation problems are among the most frequently asked in interviews, and practicing them builds comfort with Java's built-in String methods.
Program 1: Check if a String is a Palindrome
javapublic class PalindromeCheck { public static void main(String[] args) { String text = "madam"; String reversed = new StringBuilder(text).reverse().toString(); System.out.println("Is palindrome: " + text.equals(reversed)); } }
Output:
Is palindrome: trueProgram 2: Count Vowels in a String
javapublic class CountVowels { public static void main(String[] args) { String text = "Java Programming"; int count = 0; for (char ch : text.toLowerCase().toCharArray()) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { count++; } } System.out.println("Vowel count: " + count); } }
Output:
Vowel count: 5Program 3: Reverse Each Word in a Sentence
javapublic class ReverseWords { public static void main(String[] args) { String sentence = "Java is fun"; String[] words = sentence.split(" "); StringBuilder result = new StringBuilder(); for (String word : words) { result.append(new StringBuilder(word).reverse()).append(" "); } System.out.println(result.toString().trim()); } }
Output:
avaJ si nufKey Points to Remember
StringBuilder'sreverse()method is a quick way to reverse text without writing manual loop logic.split()is commonly used to break a sentence into individual words for processing.- String problems are among the most common topics in coding interviews, so regular practice pays off.