Skip to content
C

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

java
public 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: true

Program 2: Count Vowels in a String

java
public 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: 5

Program 3: Reverse Each Word in a Sentence

java
public 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 nuf

Key Points to Remember

  • StringBuilder's reverse() 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.