Java Programs›Strings

🔤Java Strings Programs

String questions open almost every Java interview because they test loops, character handling and the difference between String and StringBuilder in one go. Start with the classics below, then work through the full list grouped by difficulty.53 programs · each with full code, output and a step-by-step explanation

Most Asked in Interviews

🔍
Beginner
01beginner
Capitalize First Letter of Each Word
String

Converts a sentence to title case, upper-casing each word's first letter and tidying extra spaces.

View Program →
02beginner
Check Palindrome String
String

Check whether a string reads the same forwards and backwards.

View Program →
03beginner
Check String Contains Only Alphabets
String

Determines whether a string is made up entirely of letters.

View Program →
04beginner
Check String Contains Only Digits
String

Determines whether a string is made up entirely of digits.

View Program →
05beginner
Compare Two Strings
String

Compares two strings and reports their lexicographic order.

View Program →
06beginner
Convert String to char Array
String

Splits a string into a char array with toCharArray and shows the array is an independent copy.

View Program →
07beginner
Convert String to Lowercase
String Manipulation

Converts every letter in a string to lowercase.

View Program →
08beginner
Convert String to Uppercase
String Manipulation

Converts every letter in a string to uppercase.

View Program →
09beginner
Count Vowels and Consonants in a String
String

Counts how many vowels and consonants a string contains, skipping spaces and punctuation.

View Program →
10beginner
Count Consonants in a String
String

Counts how many consonants appear in a string.

View Program →
11beginner
Count Digits in a String
String

Counts how many digit characters appear in a string.

View Program →
12beginner
Count Spaces in a String
String

Counts how many space characters appear in a string.

View Program →
13beginner
Count Special Characters in a String
String

Counts how many special characters appear in a string.

View Program →
14beginner
Count Characters Without length()
String

Counts the total number of characters in a string without using length().

View Program →
15beginner
Count Vowels in a String
String

Counts how many vowels appear in a string.

View Program →
16beginner
Count Words in a String
String

Counts how many words a sentence contains.

View Program →
17beginner
Find Longest Word in a String
String

Finds the longest word in a sentence.

View Program →
18beginner
Find Shortest Word in a String
String

Finds the shortest word in a sentence.

View Program →
19beginner
Remove Digits from a String
String Manipulation

Removes every digit character from a string.

View Program →
20beginner
Remove Spaces from a String
String Manipulation

Removes every space character from a string.

View Program →
21beginner
Remove Special Characters from a String
String Manipulation

Removes every special character from a string, keeping letters, digits, and spaces.

View Program →
22beginner
Remove Vowels from a String
String Manipulation

Removes every vowel from a string.

View Program →
23beginner
Replace Character in a String
String Manipulation

Replaces every occurrence of one character with another.

View Program →
24beginner
Replace Word in a String
String Manipulation

Replaces every occurrence of a word with another word.

View Program →
25beginner
Reverse a String
String Manipulation

Reverse a string using a loop.

View Program →
26beginner
Sort Characters in a String
String Manipulation

Sorts the characters of a string into alphabetical order.

View Program →
27beginner
Sort Words in a String
String Manipulation

Sorts the words of a sentence into alphabetical order.

View Program →
28beginner
Toggle String Case
String Manipulation

Swaps the case of every letter in a string.

View Program →
Intermediate
29intermediate
Check Anagram
String

Check whether two strings are anagrams of each other.

View Program →
30intermediate
Check Isogram
String

Determines whether a word has no repeating letters.

View Program →
31intermediate
Check Pangram
String

Determines whether a sentence contains every letter of the alphabet.

View Program →
32intermediate
Check String Rotation
String

Determines whether one string is a rotation of another.

View Program →
33intermediate
Check Substring
String

Determines whether one string appears inside another.

View Program →
34intermediate
Compress a String
String

Applies run-length encoding, replacing each run of a repeated character with the character and its count.

View Program →
35intermediate
Count Occurrences of Substring
String

Counts how many non-overlapping times a substring appears in a string.

View Program →
36intermediate
Find All Substrings of a String
String Manipulation

Generates every contiguous substring of a string.

View Program →
37intermediate
Find Character Frequency
String

Counts how many times each character appears in a string.

View Program →
38intermediate
Find Common Characters Between Two Strings
String

Finds every character that appears in both of two strings.

View Program →
39intermediate
Find Duplicate Characters in a String
String

Finds every character that appears more than once in a string.

View Program →
40intermediate
Find First Non-Repeated Character in a String
String

Finds the first character that appears exactly once in a string.

View Program →
41intermediate
Find First Repeated Character in a String
String

Finds the first character that repeats while scanning a string left to right.

View Program →
42intermediate
Find Last Non-Repeated Character in a String
String

Finds the last character, scanning from the end, that appears exactly once.

View Program →
43intermediate
Find Last Repeated Character in a String
String

Finds the last character, scanning from the end, that appears more than once.

View Program →
44intermediate
Find Least Frequent Character in a String
String

Finds the character that appears least often in a string.

View Program →
45intermediate
Find Longest Common Prefix
String

Finds the longest starting substring shared by every word in an array.

View Program →
46intermediate
Find Most Frequent Character in a String
String

Finds the character that appears most often in a string.

View Program →
47intermediate
Remove Duplicate Characters
String Manipulation

Removes repeated characters from a string, keeping each one's first occurrence.

View Program →
48intermediate
Remove Extra Spaces from a String
String Manipulation

Collapses runs of spaces down to a single space and trims the ends.

View Program →
49intermediate
Reverse Each Word in a String
String Manipulation

Reverses the letters within each word, keeping the word order intact.

View Program →
50intermediate
Reverse Words in a String
String Manipulation

Reverses the order of words in a sentence.

View Program →