This page contains Java code for below questions:
1. Reverse characters in a given string
2. Check Palindrome
3. Find unique characters(Remove duplicate characters)
4. Count the repetition of characters(find duplicate characters) in a given string
5. Create Fibonacci sequence/number
1. Reverse
characters in a given string
public static void reverseChar() {
String str = "asdfghjkl";
//lkjhgfdsa
String expStr = "";
int len = str.length();
for(int i=len-1;i>=0;i--) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
expStr = expStr+strCh;
}
System.out.println("The reverse string is : "+expStr);
}
OUTPUT:2. Check Palindrome
public static void palindrome() {
String str = "level";
//lkjhgfdsa
String expStr = "";
int len = str.length();
for(int i=len-1;i>=0;i--) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
expStr = expStr+strCh;
}
//System.out.println("The
reverse string is : "+expStr);
String strInSameOrder = "";
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
strInSameOrder = strInSameOrder+strCh;
}
//System.out.println("The
string in right/same order is : "+strInSameOrder);
if(expStr.equals(strInSameOrder)) {
System.out.println("The given string is a Palindrome.");
} else {
System.out.println("The given string is NOT a Palindrome.");
}
}
OUTPUT:
The given string is a Palindrome.
3. Find
unique characters(Remove duplicate characters): method 1 of 2
public static void uniqueChar() {
String str = "asdfsgdfsdasderfvdera";
ArrayList<String> alist = new ArrayList<>();
char ch = str.charAt(0);
String strCh = Character.toString(ch);
alist.add(strCh);
int len = str.length();
for(int i=0;i<len;i++) {
char chh = str.charAt(i);
String strChh = Character.toString(chh);
if(!alist.contains(strChh)) {
alist.add(strChh);
}
}
System.out.println("The unique characters are : "+alist);
}
OUTPUT:
The unique characters are : [a, s, d, f, g, e, r, v]
3. Find unique characters(Remove duplicate characters): method 2 of 2:
Count each characters
Find unique chars
Find non-unique(duplicate) chars
public static void countChars() {
String str = "asdazsdeggvsesaegsme";
HashMap<String,Integer> map = new HashMap<String,Integer>();
int length = str.length();
for(int i=0;i<length;i++) {
String ky = Character.toString(str.charAt(i));
if(map.containsKey(ky)) {
map.put(ky, map.get(ky)+1);
} else {
map.put(ky, 1);
}
}
System.out.println("count each characters = "+map); //output : count each characters = {a=3, s=5, d=2, e=4, v=1, g=3, z=1, m=1}
ArrayList<String> alist = new ArrayList<>(map.keySet());//stores all the keys
ArrayList<Integer> blist = new ArrayList<>(map.values());//stores all the values
ArrayList<String> clist = new ArrayList<>();//new arraylist to show the unique chars
ArrayList<String> dlist = new ArrayList<>();//new arraylist to show the non-unique chars
System.out.println("map keys = "+alist);//output: map keys = [a, s, d, e, v, g, z, m]
System.out.println("map values = "+blist);//output: map values = [3, 5, 2, 4, 1, 3, 1, 1]
//Show unique chars
for(int i=0;i<map.size();i++) {
if((blist.get(i))==1) {
clist.add(alist.get(i));
}
}
System.out.println("chars which are unique = "+clist);//output: chars which are unique = [v, z, m]
//Show duplicate(non-unique) chars
for(int i=0;i<map.size();i++) {
if(!((blist.get(i))==1)) {
dlist.add(alist.get(i));
}
}
System.out.println("chars which are not unique = "+dlist);//output: chars which are not unique = [a, s, d, e, g]
}
4. Count
the repetition of characters(find duplicate characters) in a given string
public static void duplicateChar() {
String str = "asdfsgdfsdasderfvdera";
int len = str.length();
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
if(map.containsKey(strCh)) {
map.put(strCh, map.get(strCh)+1);
} else {
map.put(strCh, 1);
}
}
System.out.println("The duplicate chars are : "+map);
}
OUTPUT:
The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=3, v=1, g=1}
5. Create
Fibonacci sequence/number
public static void fibonacciSeq() {
//
0,1,1,2,3,5,8,13,....
ArrayList<Integer> alist = new ArrayList<>();
alist.add(0);
alist.add(1);
for(int i=2;i<10;i++) {
alist.add(alist.get(i-1)+alist.get(i-2));
}
System.out.println("The Fibonacci sequence is :"+alist);
}
OUTPUT:
The
Fibonacci sequence is :[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]