Top 5 string programming interview questions in java

In this article, we are going to discuss very important programming interview questions for interviews these programming questions are asking in the written round and sometimes at a time of technical round by the interviewer.

 

 

1. Find Duplicate Characters and count duplicate characters from a string 

 In this programming interview questions, we need to find repeated characters and count of duplicate characters from a given String, Suppose String s = "Niraj Kumar Singh" now we need to find all duplicate characters from this string, so duplicate characters are n, i, r, a.

Steps for finding duplicate characters.

Step1:- First we need to convert the string to char array using the toCharArray() method.

Step2:- We are using two loops. in the first loop, we are taking the first characters and in the second loop, we are taking next all characters.

Steps3:- We are using the if condition and in that we are checking that the first loop character is equal to the second loop character or not. if both are equal then we increase the count and repeat the same until the loop is a break.

Steps4:- In this step, we are displaying duplicates characters and the count of that characters.


package practice.String; public class DuplicateCharacters { public static void main(String[] args) { String s = "Niraj Kumar Singh"; char[] chr = s.toCharArray(); int count; for (int i = 0; i < chr.length; i++) { count = 1; for (int j = i + 1; j < chr.length; j++) { if (chr[i] == chr[j] && chr[i] != ' ') { count++; chr[j] = '0'; } } if (count > 1 && chr[i] != '0') { // System.out.print(chr[i]); System.out.println(chr[i] + " is repeated :" + count); } } } }

 

2. Check String is Palindrome or not

We are Checking String is Palindrome or not Using String Buffer.


package practice.String; public class PalindromeString { public static void main(String Arg[]) { String s = "aba"; //Using StringBuffre reverse or Palindrome check StringBuffer sb = new StringBuffer(s); sb.reverse(); String reverse = sb.toString(); if (s.equals(reverse)) { System.out.println("String is Palindrome : " + s); } else { System.out.println("String is not Palindrome : " + s); } } }

 

Checking string is Palindrome without using StringBuffer or StringBuilder


package practice.String; public class PalindromeString { public static void main(String Arg[]) { String s = "aba"; //Without Using StringBuffer or StringBuilder reverse or Palindrome check String reverse=""; for(int i=s.length()-1; i>=0;i--) { reverse=reverse+s.charAt(i); } if (s.equals(reverse)) { System.out.println("String is Palindrome : " + s); } else { System.out.println("String is not Palindrome : " + s); } } }

 

3. Find duplicates words


package practice.String; public class FindDuplicatesWords { public static void main(String[] args) { int count; String s = "Raj Rahul Amit kumal Raj Munna"; s = s.toLowerCase(); String words[] = s.split(" "); for (int i = 0; i < words.length; i++) { count = 1; for (int j = i + 1; j < words.length; j++) { if (words[i].equals(words[j])) { count++; words[j] = "0"; } } if (count > 1 && words[i] != "0") { System.out.println(words[i]); } } } }

 

4. Remove duplicates from List of String


package practice.String; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class RemoveDuplicats { public static void main(String Arg[]) { List<String> ls = new ArrayList<String>(); ls.add("Rahul"); ls.add("Rahul"); ls.add("Amit"); ls.add("Niraj"); ls.add("kunal"); Set<String> st = new LinkedHashSet<String>(ls); System.out.println(ls); System.out.println(st); } }

 

5. Reverse String


package practice.String; public class ReverseString { public static void main(String Arg[]) { String s = "aba"; StringBuffer sb = new StringBuffer(s); sb.reverse(); String reverse = sb.toString(); if (s.equals(reverse)) { System.out.println("String is Reverse : " + s); } else { System.out.println("String is not Reverse : " + s); } } }

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

Your email address will not be published.