TCS NQT Java Coding Questions (4-10) with Explanation
4. Remove Duplicates from a String
// This program removes duplicate characters from a string.
// We use a Set to track seen characters and only append if it's not already added.
// The result is printed after looping through all characters.
import java.util.*;
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter string: ");
String str = sc.nextLine();
StringBuilder result = new StringBuilder();
Set<Character> seen = new HashSet<>();
for (char c : str.toCharArray()) {
if (!seen.contains(c)) {
seen.add(c);
result.append(c);
}
}
System.out.println("Without duplicates: " + result);
}
}
5. Frequency of Characters
// This program counts the frequency of each character in the input string.
// We use a HashMap to store each character and its frequency.
// Then we iterate and print each character with its count.
import java.util.*;
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter string: ");
String str = sc.nextLine();
Map<Character, Integer> freq = new HashMap<>();
for (char c : str.toCharArray()) {
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
System.out.println("Character Frequencies:");
for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
6. Longest Word in a Sentence
// We split the input sentence by spaces to get words.
// Then we loop through each word and keep track of the longest one found.
// Finally, we print the longest word.
import java.util.Scanner;
public class LongestWord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
String longest = "";
for (String word : words) {
if (word.length() > longest.length()) {
longest = word;
}
}
System.out.println("Longest word: " + longest);
}
}
7. Anagram Check
// Two strings are anagrams if they have the same characters in a different order.
// We sort both strings and compare them. If equal, they are anagrams.
import java.util.*;
public class AnagramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String s1 = sc.nextLine();
System.out.print("Enter second string: ");
String s2 = sc.nextLine();
char[] a = s1.toCharArray();
char[] b = s2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
if (Arrays.equals(a, b)) {
System.out.println("Anagrams");
} else {
System.out.println("Not Anagrams");
}
}
}
8. Convert String to Title Case
// The first letter of each word is capitalized, and the rest are in lowercase.
// We split the sentence into words, capitalize the first letter, and combine.
import java.util.Scanner;
public class TitleCase {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1).toLowerCase()).append(" ");
}
System.out.println("Title Case: " + result.toString().trim());
}
}
9. Count Words in a String
// We split the sentence using space and count the number of words.
// trim() removes leading/trailing spaces, split("\\s+") handles multiple spaces.
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.trim().split("\\s+");
System.out.println("Word count: " + words.length);
}
}
10. Remove Spaces and Special Characters
// This code removes all characters that are not letters or digits.
// Regex [^a-zA-Z0-9] means 'not a letter or number'.
import java.util.Scanner;
public class CleanString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter string with special characters: ");
String input = sc.nextLine();
String cleaned = input.replaceAll("[^a-zA-Z0-9]", "");
System.out.println("Cleaned String: " + cleaned);
}
}