Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

ProgramsJava

Java String Programs

Learn Java string programs for reversing text, counting letters, checking palindromes, and more. See full code, output, common mistakes, and practice questions.

25 programs · open one, then the next

Start with Reverse String
  1. 01Reverse StringBeginnerReverse a full line in Java by joining charAt values in a backward loop. Spaces stay in the result; char-based reversal is not safe for every emoji.Open example →
  2. 02Check Palindrome StringBeginnerTest a text palindrome in Java with replaceAll, lowercase conversion, StringBuilder.reverse, and equals. Whitespace and case are ignored; punctuation stays.Open example →
  3. 03Count VowelsBeginnerCount vowels in Java using a charAt loop and a helper with Character.toLowerCase. Each a, e, i, o, or u counts once per occurrence; accents are not included.Open example →
  4. 04Count ConsonantsBeginnerCount consonants in Java by combining English letter-range checks with a vowel helper. Repeated letters count separately; spaces and digits are ignored.Open example →
  5. 05Count WordsBeginnerCount words in Java with trim and split on one or more whitespace characters. Empty input returns zero; punctuation remains attached to its word.Open example →
  6. 06Remove WhitespaceBeginnerRemove whitespace in Java with replaceAll and a whitespace regex. Scanner.nextLine keeps the full input; spaces and tabs vanish, while other text stays.Open example →
  7. 07Remove Duplicate CharactersIntermediateKeep first-seen characters in Java with LinkedHashSet, then build text with StringBuilder. Input order stays intact; uppercase and lowercase remain distinct.Open example →
  8. 08Anagram CheckIntermediateCompare anagrams in Java by removing whitespace, lowercasing, and sorting char arrays with Arrays.sort. Letter counts must match; punctuation is kept.Open example →
  9. 09String RotationIntermediateCheck a string rotation in Java by testing equal lengths and searching doubled text with contains. Case and spaces matter; unchanged text counts too.Open example →
  10. 10Frequency of CharactersIntermediateCount character frequency in Java with LinkedHashMap and getOrDefault. Print totals in first-seen order; spaces and uppercase letters have their own counts.Open example →
  11. 11Longest WordBeginnerFind the longest word in Java by splitting whitespace and comparing lengths in a loop. The first word wins a tie, and attached punctuation adds to length.Open example →
  12. 12Shortest WordBeginnerChoose the shortest word in Java with trim, split, and a loop starting from the first word. Empty input prints No words; equal lengths keep the first match.Open example →
  13. 13Replace SubstringBeginnerReplace every literal substring in Java with String.replace after reading three full lines. Matching is case-sensitive; save the returned text to keep changes.Open example →
  14. 14Reverse Words in SentenceBeginnerReverse word order in Java with split and a backward loop into StringBuilder. Letters stay unchanged, but groups of spaces and tabs become single spaces.Open example →
  15. 15Sort CharactersBeginnerSort text characters in Java by calling toCharArray, Arrays.sort, and new String. Duplicates remain; mixed letter case follows stored values, not a dictionary.Open example →
  16. 16Convert to Title CaseBeginnerFormat title case in Java with lowercase conversion, Character.toUpperCase, and StringBuilder. Each word gets a capital; NASA becomes Nasa under this rule.Open example →
  17. 17Count Uppercase LettersBeginnerCount capital letters in Java with a charAt loop and Character.isUpperCase. The helper recognises many non-English letters; spaces and digits add nothing.Open example →
  18. 18Count Lowercase LettersBeginnerMeasure lowercase occurrences in Java using Character.isLowerCase in a char loop. Hello gives four, counting both l letters; the input itself is unchanged.Open example →
  19. 19Extract NumbersBeginnerCollect digit characters in Java with Character.isDigit and StringBuilder. Leading zeros remain, but signs and decimal points are discarded, not parsed.Open example →
  20. 20Validate Email (Regex)IntermediateCheck email-like text in Java with Pattern.matches and a small regex. Spaces fail, but a@b passes; this format test does not confirm an address exists.Open example →
  21. 21Validate PasswordIntermediateTest password format in Java with regex lookaheads for a digit, both letter cases, and eight characters. Passing the pattern does not prove a password is safe.Open example →
  22. 22Check PangramIntermediateDetect an English pangram in Java by lowercasing text and collecting a-z in a HashSet. A size of 26 means every letter appeared; repeated letters add nothing.Open example →
  23. 23Split & Join StringsBeginnerSplit words on whitespace in Java and join them with hyphens using StringJoiner. No final hyphen is added; punctuation and letter case are left unchanged.Open example →
  24. 24String CompressionIntermediateCompress character runs in Java with a loop and StringBuilder, turning aaabbc into a3b2c1. Keep the final run; this encoding can make short text longer.Open example →
  25. 25String PermutationsAdvancedPrint string permutations in Java with recursive prefix building and substring. The empty remainder stops each branch; repeated letters give duplicate lines.Open example →