Interview Question based on String

/*find all the permutations of a string*/
public class PrintAllPermutations {
public static void permutation(String name, String blank) {
if (name.length() == 0) {
System.out.println(blank);
}
for (int i = 0; i < name.length(); i++) {
String newBlank = blank + name.charAt(i);
String newRemaining =.name.substring(0, i) + name.substring(i + 1);
permutation(newRemaining, newBlank);
}
}public static void main(String[] args) {
PrintAllPermutations.permutation(“ABCX”, “”);
}} Op T

/*Java Program to count vowels and consonants in String*/

package com.string;

public class TotalCountVowelsAndConsonants {
public static String count(String name) {
int length = name.trim().length();
int vowels ,consonants;
char[] ch = name.trim().toCharArray();
int count = 0;
for (char c : ch) {
switch (c){
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
count++;
break;
default:
}}
vowels = count;
consonants = length — vowels; saa
return “vowels” + “=” + vowels + “ “ + “consonants” + “=” + consonants;}
public static void main(String[] args)r {
String count = TotalCountVowelsAndConsonants.count(“sanjayaaaaaaaaaaaa”);
System.out.println(count);}}

/*How to check if two String are Anagram in Java
if they contains same characters but on different order e.g. army and mary, stop and pots etc. Anagrams*/
package com.string;

import java.util.Arrays;
public class StringIsAnagram {
public static String anagramTest(String first, String second) {
char[] fch = first.toLowerCase().toCharArray();
char[] sch = second.toLowerCase().toCharArray();
Arrays.sort(fch);
Arrays.sort(sch);
boolean status = true;
status = Arrays.equals(fch, sch);
if (first.length() != second.length()) {
return “No Anagram”;
} else if (status) {
return “Anagram”;
}
return “No Anagram”;
}public static void main(String[] args) {
String test = StringIsAnagram.anagramTest(“sanjay”, “Snajya”);
System.out.println(test);
}}

How to reverse String in Java using

public class ReverseString {

public static String reverseString(String name) {
String reverse = “”;
for (int i = name.length() — 1; i >= 0; i — ) {
reverse = reverse + name.charAt(i);}return reverse;
}public static void main(String[] args) {
String name = ReverseString.reverseString(“Sanjay”);
System.out.println(name);
}}

FindDuplicateCharacters

package com.string;
import java.util.HashMap;
import java.util.Map;
public class FindDuplicateCharacters {
/*find Repeated Characters of String*/
public static Map<Character, Integer> dublicate(String name) {
char[] ch = name.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : ch) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);}}
return map;}
public static void main(String[] args) {
System.out.println(FindDuplicateCharacters.dublicate(“sanjay”));
}}

/*Write a Java program to find the first non-repeated character in a String*/

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class FirstNonRepeatedCharacter {
public static Map<Character, Integer> nonRepeatedCharacter(String name) {
char[] ch = name.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<>();
for (char c : ch) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);}}
return map;}
public static void main(String[] args) {
Map<Character, Integer> map = FirstNonRepeatedCharacter.nonRepeatedCharacter(“asnsanjay”);
Set<Entry<Character, Integer>> set = map.entrySet();
for (Entry<Character, Integer> entry : set) {
if (entry.getValue() == 1) {
System.out.println(entry.getKey());
break;

}}}}

String containing first letter of every word in a given string with spaces
input-”String containing first letter o” — — output is : Scflo

public class TrickTest {
public static String action(String name) {
int length = name.length();
String box = “”;
boolean space = true;
for (int i = 0; i < length; i++) {
if (name.charAt(i) == ‘ ‘) {
space = true;
} else if (name.charAt(i) != ‘ ‘ && space == true) {
box += name.charAt(i);
space = false;
}
}
return box;

}
public static void main(String[] args) {
String t1 = action(“String containing first letter o”);
System.out.println(t1);}}