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- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { private static boolean isVowel(char c) {
- 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 →javaimport java.util.Scanner; public class Main { private static boolean isAlphabet(char c) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.LinkedHashSet; import java.util.Scanner; import java.util.Set;
- 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 →javaimport java.util.Arrays; import java.util.Scanner; public class Main {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner;
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Arrays; import java.util.Scanner; public class Main {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 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 →javaimport java.util.Scanner; import java.util.regex.Pattern; public class Main {
- 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 →javaimport java.util.Scanner; import java.util.regex.Pattern; public class Main {
- 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 →javaimport java.util.HashSet; import java.util.Scanner; import java.util.Set;
- 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 →javaimport java.util.Scanner; import java.util.StringJoiner; public class Main {
- 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 →javaimport java.util.Scanner; public class Main { public static String compress(String s) {
- 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 →javaimport java.util.Scanner; public class Main { private static void permute(String s, String prefix) {