Top 5 string programming interview questions in java

In almost all interview these programming questions are asking by interviewer.

 

1. Find Duplicate Characters from string


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); } } }

 

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. Required fields are marked *