00:00
/* Reverse a String*/ public class M06 { public static void main(String[] args) { String s1 = "rajat singh"; String s2 = ""; for(int i=s1.length()-1;i>=0;i--){ s2=s2+s1.charAt(i); } System.out.println(s2); } }
/* String palandrom */ public class M06 { public static void main(String[] args) { String s1 = "madam"; String s2 = ""; for(int i=s1.length()-1;i>=0;i--){ s2=s2+s1.charAt(i); } if(s2.equals(s1)){ System.out.println("string is palandrom"); } else{ System.out.println("string is not palandrom"); } } }
/* Maximum Occurring Character in String - 1 */ public class M06 { public static void main(String[] args) { String s = "this is my city and i keep it clean"; s=s.replaceAll("\\s", ""); System.out.println(s); int[] arr = new int[127]; for(int i=0; i<s.length(); i++){ arr[s.charAt(i)] = arr[s.charAt(i)]+1; } int max = -1; char c = ' '; for (int i=0;i<s.length();i++){ if(max<arr[s.charAt(i)]){ // one by one compair value with max max=arr[s.charAt(i)]; // if found update c=s.charAt(i); // put the value in c , aging loop and check if found max update c , otherwise not } } System.out.println("maximum repeated character is = "+c); } }
/* Maximum Occurring Character in String - 2 */ import java.util.HashMap; import java.util.Map; public class M06 { public static void main(String[] args) { String s = "abbccc"; HashMap<Character,Integer> hm = new HashMap<>(); char [] c =s.toCharArray(); for(char ch : c){ if(hm.containsKey(ch)){ hm.put(ch, hm.get(ch)+1); } else{ hm.put(ch, 1); } } int maxCount = 0; char maxChar = ' '; for(Map.Entry <Character,Integer>me :hm.entrySet()){ if(maxCount < me.getValue()){ maxCount= me.getValue(); maxChar=me.getKey(); } } System.out.println("max repeated = "+maxChar); } } /* Computers can understand only the numeric values. But, it is not always certain that all the inputs are given in numeric form. So, there was a need for an encoding system which could convert the text files into numeric values. For this ASCII was developed. AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE (ASCII) is an encoding approach, which is a code for representing 128 characters in English into seven bit integers. */
Home PREVIOUS NEXT