
- Reverse a string using Stack in Java
import java.util.*; public class QATechTools { static String reverseAStringUsingStack(String str){ Stack myStack = new Stack(); char[] chars = str.toCharArray(); for(char c:chars){ myStack.push(c); } StringBuilder sb=new StringBuilder(); while (!myStack.isEmpty()) { sb.append(myStack.pop()); } return sb.toString(); } public static void main(String args[]){ System.out.println(reverseAStringUsingStack("qatechtools")); } } //Output sloothcetaq
2. Find the first repeating character in the Given string
import java.util.*; public class QATechTools { static char firstRepeatingChar(char str[]){ HashSet<Character> h= new HashSet<>(); for (int i =0 ; i <str.length-1;i++){ char c = str[i]; if(h.contains(c)){ return c; } else{ h.add(c); } } return '\0'; } public static void main(String args[]){ String stringToBeVerified= "QAtechtools"; char[] check= stringToBeVerified.toCharArray(); System.out.println(firstRepeatingChar(check)); } } //output t
3. Get count of character in a given String
import java.util.*; public class QATechTools { static void getCountRepeatedCharacter(String str){ HashMap<Character,Integer> charCount = new HashMap<Character,Integer>(); char[] chars= str.toCharArray(); for (char c:chars){ if(charCount.containsKey(c)){ charCount.put(c,charCount.get(c) + 1); } else{ charCount.put(c,1); } } for (Map.Entry entry : charCount.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } public static void main(String args[]){ String stringToBeVerified= "QAtechtools"; getCountRepeatedCharacter(stringToBeVerified); } } //output Q 1 A 1 c 1 s 1 t 2 e 1 h 1 l 1 o 2
4. Find the index of the given char in the string
import java.util.*; public class QATechTools { static int findCharIndexGivenChar(char str[], char x){ int h = -1; for (int i= 0; i<str.length;i++){ if(str[i]==x){ h=i; break; } } return h; } public static void main(String args[]){ String stringToBeVerified= "QAtechtools"; char[] check= stringToBeVerified.toCharArray(); System.out.println(findCharIndexGivenChar(check,'l')); } } //output 9
5 . Print Collatz sequence
Starting with any positive integer N, Collatz sequence is defined corresponding to n as the numbers formed by the following operations :
- If n is even, then n = n / 2.
- If n is odd, then n = 3*n + 1.
- Repeat above steps, until it becomes 1.
import java.util.*; public class QATechTools { static void printCollatzSequence(int n) { while (n != 1) { System.out.print(n + " "); // If n is odd if ((n & 1) == 1) n = 3 * n + 1; // If even else n = n / 2; } System.out.print(n); } public static void main(String[] args) { printCollatzSequence(12); } } //output 12 6 3 10 5 16 8 4 2 1
6. Find the minimum no of rotation to find the original string
import java.util.*; public class QATechTools { static int findRotations(String str) { String tmp = str + str; int n = str.length(); for (int i = 1; i <= n; i++) { // substring from i index of original // string size. String substring = tmp.substring(i, str.length()); // if substring matches with original string // then we will come out of the loop. if (str == substring) return i; } return n; } public static void main(String[] args) { System.out.println(findRotations("QAtechtools")) ; } } //output 11