Top 15 Java String Coding Interview Questions
Top 15 Java String Coding Interview Questions (With Answers)
1. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String input = "InterviewYatra";
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed: " + reversed);
}
}
2. Check if a String is a Palindrome
public class PalindromeCheck {
public static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i++) != str.charAt(j--)) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPalindrome("madam")); // true
}
}
3. Count Vowels and Consonants
public class VowelConsonantCount {
public static void main(String[] args) {
String str = "InterviewYatra";
int vowels = 0, consonants = 0;
for (char ch : str.toLowerCase().toCharArray()) {
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1) vowels++;
else consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
}
4. Remove Duplicate Characters
public class RemoveDuplicates {
public static void main(String[] args) {
String input = "interview";
String result = "";
for (int i = 0; i < input.length(); i++) {
if (!result.contains(String.valueOf(input.charAt(i)))) {
result += input.charAt(i);
}
}
System.out.println("Unique: " + result); // intervw
}
}
5. Check if Two Strings Are Anagrams
import java.util.Arrays;
public class AnagramCheck {
public static boolean isAnagram(String s1, String s2) {
char[] a = s1.toCharArray();
char[] b = s2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
return Arrays.equals(a, b);
}
}
6. Character Frequency Count
import java.util.HashMap;
public class CharacterFrequency {
public static void main(String[] args) {
String str = "developer";
HashMap<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
System.out.println(map);
}
}
7. First Non-Repeating Character
public class FirstUniqueChar {
public static void main(String[] args) {
String str = "aabbcde";
for (char c : str.toCharArray()) {
if (str.indexOf(c) == str.lastIndexOf(c)) {
System.out.println("First Non-Repeating: " + c);
break;
}
}
}
}
8. Replace Spaces with Hyphens
public class ReplaceSpaces {
public static void main(String[] args) {
String sentence = "Java Developer";
System.out.println(sentence.replace(" ", "-"));
}
}
9. Convert to Title Case
public class TitleCase {
public static void main(String[] args) {
String[] words = "java developer job".split(" ");
StringBuilder sb = new StringBuilder();
for (String word : words) {
sb.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1)).append(" ");
}
System.out.println(sb.toString().trim());
}
}
10. String Contains Only Digits
public class OnlyDigits {
public static void main(String[] args) {
String input = "12345";
boolean result = input.matches("\\\\d+");
System.out.println("Only digits: " + result);
}
}
11. Print All Substrings
public class Substrings {
public static void main(String[] args) {
String str = "abc";
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j <= str.length(); j++) {
System.out.println(str.substring(i, j));
}
}
}
}
12. String Rotation Check
public class RotationCheck {
public static void main(String[] args) {
String s1 = "abcd";
String s2 = "cdab";
System.out.println((s1.length() == s2.length()) && ((s1 + s1).contains(s2)));
}
}
13. Find Longest Word
public class LongestWord {
public static void main(String[] args) {
String sentence = "Java backend developer interview questions";
String[] words = sentence.split(" ");
String longest = "";
for (String word : words) {
if (word.length() > longest.length()) longest = word;
}
System.out.println("Longest Word: " + longest);
}
}
14. Compare Strings Without equals()
public class CompareStrings {
public static void main(String[] args) {
String s1 = "Interview";
String s2 = "Interview";
boolean same = (s1.compareTo(s2) == 0);
System.out.println("Same: " + same);
}
}
15. Reverse Words in Sentence
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Java is great";
String[] words = sentence.split(" ");
String reversed = "";
for (int i = words.length - 1; i >= 0; i--) {
reversed += words[i] + " ";
}
System.out.println("Reversed Words: " + reversed.trim());
}
}
0 Comments