Java experience interview programming questions on Strings


1.Java Basic interview Programs on Strings


 2.Reverse a String Without using String API?





 3.Sorting the String without using String API?


 4.Check String is palindrome or not? 

 5.How to split a String in java

  • #1: Java Program to split a String using String Split() method:
  • #2: Java Program to split a String using String Split() method and use for each loop to display  
  • #4: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
  • Check here for all questions on Splitting a string using string  method

6.Removing Non Ascii character from a String?


 7.Java program to replace or remove a character in a string java



8.Java Program to check two strings are anagrams or not

 9.Java program to return maximum occurring character in the input string

10.Java program to count the number of vowels in a string.




11.Java Program to find numbers of occurrences of given char in a string


12.Java Program to count number of words in a String.
13.How to reverse vowels in a string java
14.Java program to remove vowels from string

 15.How to find count of uppercase letters in a string 

Remove non ascii character from string

#1: Java Program to Remove non ASCII chars from String

  1. package com.instanceofjava;
  2.  
  3. class RemoveNonASCIIString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance��of��java";
  7.   System.out.println(str);
  8.  
  9.   str = str.replaceAll("[^\\p{ASCII}]", "");
  10.  
  11.   System.out.println("After removing non ASCII chars:");
  12.  
  13.   System.out.println(str);
  14. }
  15. }
Output:
  1. Instance��of��java
  2. After removing non ASCII chars:
  3. Instanceofjava


#2: Java Program to Remove multiple spaces in a string

  1. package com.instanceofjava;
  2.  
  3. class RemoveNonASCIIString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance  of    java";
  7.   StringTokenizer st = new StringTokenizer(str, " ");
  8.  
  9.   StringBuffer sb = new StringBuffer();
  10.  
  11.   while(st.hasMoreElements()){
  12.          sb.append(st.nextElement()).append(" ");
  13.   }
  14.  
  15.      System.out.println(sb.toString().trim());
  16.     
  17.     }
  18. }
  19. }


Output:
  1. Instance of java

How to split a string in java


#1: Java Program to split a String using String Split() method:

  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance-of-Java";
  7.   String strarray[]=str.split("-");
  8.  
  9.     for (int i = 0; i < strarray.length; i++) {
  10.         System.out.println(strarray[i]);
  11.     }
  12.    
  13. }
  14. }
Output:
  1. Instance
  2. of
  3. Java

#2: Java Program to split a String using String Split() method and use for each loop to display
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.     String strarray[]=str.split("-");
  9.  
  10.     for (String string : strarray) {
  11.         System.out.println(string);
  12.     }
  13.  
  14. }
  15. }


Output:
  1. Instance
  2. of
  3. Java



#3: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.    if(str.contains("-")){
  9.  
  10.     String strarray[]=str.split("-");
  11.  
  12.     for (String string : strarray) {
  13.         System.out.println(string);
  14.     }
  15.  
  16.     }
  17.     else{
  18.         System.out.println("String does not conatain specified char so splitting is not possible");
  19.     }
  20.  
  21. }
  22. }
Output:
  1. Instance
  2. of
  3. Java

#4: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.    if(str.contains(".")){
  9.  
  10.     String strarray[]=str.split("-");
  11.  
  12.     for (String string : strarray) {
  13.         System.out.println(string);
  14.     }
  15.  
  16.     }
  17.     else{
  18.         System.out.println("String does not contains specified char so splitting is not possible");
  19.     }
  20.  
  21. }
  22. }
Output:
  1. String does not contains specified char so splitting is not possible

Remove Character from string in java


#1: Java Program to Remove all occurrences of a string 

  1. package com.instanceofjava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.     str = str.replace("a", "");
  8.     System.out.println(str);
  9. }
  10. }
Output:
  1. Jv

#2: Java Program to Replace First occurance of Specific index char in a String

  1. package com.instanceofjava;
  2.  
  3. class RemoveCharString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Java";
  7.  
  8. //String result = str.substring(0, index) + str.substring(index+1);
  9.  
  10.   String result = str.substring(0, 1) + str.substring(1+1);
  11.   System.out.println(result);
  12.  
  13. }
  14. }


Output:
  1. Jva


#3: Java Program to Remove all Numbers from a string.

  1. package com.instanceofjava;
  2.  
  3. class RemoveNumberString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance12ofjava143";
  7.   str = str.replaceAll("[0-9]","")
  8.   System.out.println(str);
  9.  
  10. }
  11. }
Output:
  1. Instanceofjava

Java Program to find Max occurred character in a string


  • How to print duplicate characters from string in java?
  • Java program to count number of repeated characters in a string.
  • Java program for printing a repetitive letters in a string
  • count occurrences of each character in string java


  1. package com.javatutorial;
  2.  
  3. public class MaxOccuranceOfChar{
  4.    
  5. public static String MaxOccuredChar(String str) {
  6.  
  7.         char[] array = str.toCharArray();
  8.         int maxCount = 1;
  9.         char maxChar = array[0];
  10.  
  11.   for(int i = 0, j = 0; i < str.length() - 1; i = j){
  12.        int count = 1;
  13.    while (++j < str.length() && array[i] == array[j]) {
  14.           count++;
  15.         }
  16.  
  17.   if (count > maxCount) {
  18.      maxCount = count;
  19.      maxChar = array[i];
  20.  }
  21.  
  22.   }
  23.  
  24.         return (maxChar + " = " + maxCount);
  25.  }

  26.  public static void main(String[] args) {
  27.   
  28.   String str1=MaxOccuredChar("instanceofjava");
  29.   System.out.println(str1);
  30.  
  31.   String str2=MaxOccuredChar("aaaabbbccc");
  32.   System.out.println(str2);
  33.  
  34.   String str3=MaxOccuredChar("ssssiiinnn");
  35.   System.out.println(str3);
  36.  
  37.   String str4=MaxOccuredChar("jaaaavvvvvvvvaaaaaaaaaa");
  38.   System.out.println(str4);
  39.  
  40. }

  41. }


Output:

  1. i = 1
  2. a = 4
  3. s = 4
  4. a = 10
     

Java program to check two strings are anagrams



  1. package com.javatutorial;
  2.  import java.util.Arrays;

  3. public class CheckAnagramStrings {
  4.   
  5. private static boolean isAnagram(String str1, String str2) {
  6.  
  7.         if (str1.length() != str2.length()) {
  8.             return false;
  9.         }
  10.         str1 = sortCharacters(str1);
  11.         str2 = sortCharacters(str2);
  12.         return str1.equals(str2);
  13.  }
  14.  
  15. private static String sortCharacters(String str) {
  16.         char[] charArray = str.toLowerCase().toCharArray();
  17.         Arrays.sort(charArray);
  18.         return String.valueOf(charArray);
  19.  }

  20.  public static void main(String[] args) {
  21.    
  22.  String str1 = "bemru";
  23.  String str2 = "mureb";
  24.  
  25.  if (isAnagram(str1, str2)) {
  26.           System.out.println(str2 + " is anagram of " + str1);
  27.   } else {
  28.           System.out.println("Strings are not anagrams.");
  29.   }
  30.  
  31. }

  32. }


Output:

  1. mureb is anagram of bemru
     

Select Menu