This page contains Java code for below problems:
16. Reverse each word in a sentence, such as: There are 365 days in a year.
will be : erehT era 563 syad ni a .raey
17. Factorial of a given value
18. Check if the given string is Anagrams or not.
19. Find the first non-repating char from the given string.
20. Find the 2nd most repeated char from the given string.
16. Reverse each word in a sentence, such as: There are 365 days in a year.
will be : [erehT, era, 563, syad, ni, a, .raey]
public static void main(String[] Args) {
Scanner scr = new Scanner(System.in);
System.out.println("Enter the text");
String str = scr.nextLine();
String[] strArr = str.split(" ");
int len = strArr.length;
String[] strArrRev = new String[len];
for(int i=0;i<len;i++) {
int leni=strArr[i].length();
String strj = "";
for(int j=leni-1;j>=0;j--) {
strj = strj+ strArr[i].charAt(j);
}
System.out.println("strj = "+strj);
strArrRev[i]=strj;
}
System.out.println("Reverse Array = "+Arrays.toString(strArrRev));
}
17. Factorial of a given value
public static void factorial() {
int n = 5;
int multiply = 1;
for(int i=1;i<=n;i++) {
multiply = multiply*i;
}
System.out.println("factorial value = "+multiply);
}
Output:18. Check if the given string is Anagrams or not.
public static void anagrams () {
String str1 = "today's Sunday.";
String str2 = "sud&atos y'ny @ad.";
String str11 = str1.replaceAll("[^a-zA-Z]", "");
String str22 = str2.replaceAll("[^a-zA-Z]", "");
String[] strArr1 = new String[str11.length()];
String[] strArr2 = new String[str22.length()];
System.out.println("array 1 "+str11);
System.out.println("array 2 "+str22);
for(int i=0;i<str11.length();i++) {
String star1 = Character.toString(str11.charAt(i));
star1 = star1.toLowerCase();
if(!(star1.equals(""))){
strArr1[i] = star1;
}
}
for(int i=0;i<str22.length();i++) {
String star2 = Character.toString(str22.charAt(i));
star2 = star2.toLowerCase();
if(!(star2.equals(""))){
strArr2[i] = star2;
}
}
System.out.println("array 1 "+Arrays.toString(strArr1));
System.out.println("array 2 "+Arrays.toString(strArr2));
Arrays.sort(strArr1);
Arrays.sort(strArr2);
System.out.println("sorted array 1 "+Arrays.toString(strArr1));
System.out.println("sorted array 2 "+Arrays.toString(strArr2));
//if(strArr1.equals(strArr2)) { This is wrong for Arrays
if(Arrays.equals(strArr1, strArr2)) {
System.out.println("The given string is anagrams");
} else {
System.out.println("The given string is NOT anagrams ");
}
}
Output:
array 1 todaysSunday
array 2 sudatosynyad
array 1 [t, o, d, a, y, s, s, u, n, d, a, y]
array 2 [s, u, d, a, t, o, s, y, n, y, a, d]
sorted array 1 [a, a, d, d, n, o, s, s, t, u, y, y]
sorted array 2 [a, a, d, d, n, o, s, s, t, u, y, y]
The given string is anagrams
19. Find the first non-repating char from the given string.
public static void findFirstNonRepeatingChar() {
String str = "ASDFasdmaessi";
int len = str.length();
String stri = "";
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
String strii = Character.toString(str.charAt(i));
stri = strii.toLowerCase();
if(map.containsKey(stri)) {
map.put(stri, map.get(stri)+1);
} else {
map.put(stri, 1);
}
}
System.out.println("map = "+map);
ArrayList<String> alist = new ArrayList<String>(map.keySet());
ArrayList<Integer> blist = new ArrayList<Integer>(map.values());
for(int i=0;i<alist.size();i++) {
if(blist.get(i)==1) {
System.out.println("first non repeating char = "+alist.get(i));
break ;
}
}
}
Output:
map = {a=3, s=4, d=2, e=1, f=1, i=1, m=1}
first non repeating char = e
20. Find the 2nd most repeated char from the given string.
public static void secondMostRepeatingChar() {
String str = "asdfsdfgsdfgwfvaeagdesawjnw";
int len = str.length();
String str1="";
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
str1 = Character.toString(str.charAt(i));
if(map.containsKey(str1)) {
map.put(str1, map.get(str1)+1);
} else {
map.put(str1, 1);
}
}
System.out.println("HashMap = "+map);
//find the most repeated char or chars
ArrayList<String> alist = new ArrayList<>(map.keySet());
ArrayList<Integer> blist = new ArrayList<>(map.values());
int lenAL = alist.size();
int maxCT = 0;
for(int i=0;i<lenAL;i++) {
if(blist.get(i)>maxCT) {
maxCT = blist.get(i);
}
}
System.out.println("1st largest value for repeatition = "+maxCT);
// find the second most repeated char or chars
//int maxCT2 = blist.get(0);
int maxCT2 = 0;
for(int i=0;i<lenAL;i++) {
int blistVal = blist.get(i);
if(blistVal>maxCT2&&blistVal<maxCT) {
maxCT2 = blistVal;
}
}
System.out.println("2nd largest value for repeatition = "+maxCT2);
ArrayList<String> clist = new ArrayList<>();
for(int i=0;i<lenAL;i++) {
if(blist.get(i)==maxCT2) {
System.out.println(alist.get(i));
clist.add(alist.get(i));
}
}
System.out.println("2nd most repeated char = "+clist);
}
Output:
HashMap = {a=4, s=4, d=4, e=2, f=4, v=1, g=3, w=3, j=1, n=1}
1st largest value for repeatition = 4
2nd largest value for repeatition = 3
g
w
2nd most repeated char = [g, w]