Member-only story
Interview Question Based On String
1 min readMay 21, 2022
1-Reverse the String in java
public class Testing {
public static String getRevrse(String name) {
String rev = "";
for (int i = name.length() - 1; i >= 0; i - ) {
rev = rev + name.charAt(i);
}
return rev;
}
public static void main(String[] args) {
String name = Testing.getRevrse("SANJAY");
System.out.println(name);
}}
How to Print duplicate characters from String?
input :SAANNJAY
o/p S=1 A=3 N=2 Y=1
public class Testing {
/* How to Print duplicate characters from String? */
public static Map<Character, Integer> getCout(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) {
Map<Character, Integer> map = Testing.getCout("SANNJAYYY");
Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}}}