Thursday, 28 September 2017

Top 5 String Interview Questions : In Java

1.Write a Java Program to count vowels in a String. It accept a String from command prompt

Program :

public class VowelCounter {

     public static void main(String args[]) {
          System.out.println("Please enter some text");
          Scanner reader = new Scanner(System.in);                 

      String input = reader.nextLine();
          char[] letters = input.toCharArray();

      int count = 0;
    
      for (char c : letters) {
           switch (c) {
              case 'a':
                  case 'e':
                  case 'i':
                  case 'o':
                  case 'u':
                      count++;
          break;
          default:
                  // no count increment
          }

       }
            System.out.println("Number of vowels in String [" + input + "] is : " + count);
     }

}

Output :

Please enter some text
How many vowels in this String
Number of vowels in String [How many vowels in this String] is

2.Write a Program to reverse a String

Program :

public class StringReverseExample {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        //original string
        String str = "Sony is going to introduce Internet TV soon";
        System.out.println("Original String: " + str);

        //reversed string using Stringbuffer
        String reverseStr = new StringBuffer(str).reverse().toString();
        System.out.println("Reverse String in Java using StringBuffer: " + reverseStr);

        //iterative method to reverse String in Java
        reverseStr = reverse(str);
        System.out.println("Reverse String in Java using Iteration: " + reverseStr);

        //recursive method to reverse String in Java
        reverseStr = reverseRecursively(str);
        System.out.println("Reverse String in Java using Recursion: " + reverseStr);

    }

    public static String reverse(String str) {
        StringBuilder strBuilder = new StringBuilder();
        char[] strChars = str.toCharArray();

        for (int i = strChars.length - 1; i >= 0; i--) {
            strBuilder.append(strChars[i]);
        }

        return strBuilder.toString();
    }

    public static String reverseRecursively(String str) {

        //base case to handle one char string and empty string
        if (str.length() < 2) {
            return str;
        }

        return reverseRecursively(str.substring(1)) + str.charAt(0);

    }
}

3.Write a Java Programs for String Anagram Example.

Program :

public class AnagramCheck {
  
  
    public static boolean isAnagram(String word, String anagram){      
        if(word.length() != anagram.length()){
            return false;
        }
      
        char[] chars = word.toCharArray();
      
        for(char c : chars){
            int index = anagram.indexOf(c);
            if(index != -1){
                anagram = anagram.substring(0,index) + anagram.substring(index +1, anagram.length());
            }else{
                return false;
            }          
        }
      
        return anagram.isEmpty();
    }
  
 

4.Write a  Java Program to find duplicate characters in String.

Program :

public class FindDuplicateCharacters{

    public static void main(String args[]) {
        printDuplicateCharacters("Programming");
        printDuplicateCharacters("Combination");
        printDuplicateCharacters("Java");
    }

    /*
     * Find all duplicate characters in a String and print each of them.
     */
    public static void printDuplicateCharacters(String word) {
        char[] characters = word.toCharArray();

        // build HashMap with character and number of times they appear in String
        Map<Character, Integer> charMap = new HashMap<Character, Integer>();
        for (Character ch : characters) {
            if (charMap.containsKey(ch)) {
                charMap.put(ch, charMap.get(ch) + 1);
            } else {
                charMap.put(ch, 1);
            }
        }

        // Iterate through HashMap to print all duplicate characters of String
        Set<Map.Entry<Character, Integer>> entrySet = charMap.entrySet();
        System.out.printf("List of duplicate characters in String '%s' %n", word);
        for (Map.Entry<Character, Integer> entry : entrySet) {
            if (entry.getValue() > 1) {
                System.out.printf("%s : %d %n", entry.getKey(), entry.getValue());
            }
        }
    }

}

Output:

List of duplicate characters in String 'Programming'
g : 2
r : 2
m : 2
List of duplicate characters in String 'Combination'
n : 2
o : 2
i : 2
List of duplicate characters in String
'Java'
a:2

No comments:

Post a Comment

Abstract Class & Interface in Java

      Abstract Class & Interface in Java         Here is the most important topic on Java Abstract class and interface, Abstraction ...