Home / Tutorial /Top 10 Java Program...
John Doe
2024-03-11
0 mins read
package com.dla; public class SecondFrequentCharacter { static final int NO_OF_CHARS = 256; public static void main(String[] args) { //String to find second frequent character String str = "DevLabsAlliance"; char res = getSecondMostFreq(str); if (res != '\0') System.out.println("Second most frequent char is " + res); else System.out.println("No second most frequent character"); } static char getSecondMostFreq(String str) { //count number of occurences of every character int[] count = new int[NO_OF_CHARS]; int i; for (i=0; i< str.length(); i++) (count[str.charAt(i)])++; // Traverse through the count[] and find second highest element. int first = 0, second = 0; for (i = 0; i < NO_OF_CHARS; i++) { /* If current element is smaller than first then update both first and second */ if (count[i] > count[first]) { second = first; first = i; } /* If count[i] is in between first and second then update second */ else if (count[i] > count[second] && count[i] != count[first]) second = i; } return (char)second; } }
Output: Second most frequent char is L
package com.dla; public class ReverseAString { public static void main(String[] args) { String s = "DevLabsAlliance"; String revS = ""; for(int i= s.length() - 1; i>=0; i-- ) { revS = revS + s.charAt(i); } System.out.println("The reversed string is "+ revS); } }
Output: The reversed string is ecnaillAsbaLveD
package com.dla; import java.util.regex.Pattern; public class ReverseWordsInString { public static void main(String[] args) { String str = "Learn Lead And Succeed in DevLabsAlliance"; Pattern p = Pattern.compile("\\s"); String[] temp = p.split(str); String rev = ""; for (int i = 0; i < temp.length; i++) { if (i == temp.length - 1) rev = temp[i] + rev; else rev = " " + temp[i] + rev; } System.out.println("The reversed string is: " + rev); } }
Output: The reversed string is: DevLabsAlliance in Succeed And Lead Learn
package com.dla; public class FrequencyOfCharacter { public static void main(String[] args) { String str = "DevLabs Alliance is awesome."; char ch = 'e'; int frequency = 0; for(int i = 0; i < str.length(); i++) { if(ch == str.charAt(i)) { ++frequency; } } System.out.println("Frequency of " + ch + " = " + frequency); } }
Output: Frequency of e = 4
package com.dla; public class SortStringInDictionaryOrder { public static void main(String[] args) { String[] words = { "Ruby", "C", "Python", "Java" }; for(int i = 0; i < 3; ++i) { for (int j = i + 1; j < 4; ++j) { if (words[i].compareTo(words[j]) > 0) { // swap words[i] with words[j] String temp = words[i]; words[i] = words[j]; words[j] = temp; } } } System.out.println("In lexicographical order:"); for(int i = 0; i < 4; i++) { System.out.println(words[i]); } } }
Output: In lexicographical order:
C
Java
Python
Ruby
package com.dla; public class ReplaceSubStringWithAnotherString { public static void main(String[] args) { String str = "Learn, Lead and Succeed in DevLabsAlliance"; String toBeReplaced = "in"; String toReplacedWith = "with"; String[] astr = str.split(toBeReplaced); StringBuffer strb = new StringBuffer(); for ( int i = 0; i <= astr.length-1; i++ ) { strb = strb.append( astr[i] ); if (i != astr.length-1) { strb = strb.append(toReplacedWith); } } System.out.println(strb); } }
Output: Learn, Lead and Succeed with DevLabsAlliance
package com.dla; public class LargestNumberInArray { public static void main(String[] args) { int[] arr={28,3,15,9,17,4,23,2}; int val=arr[0]; for(int i=0; i<arr.length; i++){ if(arr[i] > val){ val=arr[i]; } } System.out.println("Largest value in the Given Array is "+ val); } }
Output: Largest value in the Given Array is 28
package com.dla; import java.util.HashMap; public class WordCount { public static void main(String[] args) { String str = "Training Training course and certification course in Devlabs Alliance"; String[] split = str.split(" "); HashMap<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < split.length - 1; i++) { if (map.containsKey(split[i])) { int count = map.get(split[i]); map.put(split[i], count + 1); } else { map.put(split[i], 1); } } System.out.println(map); } }
Output: {Training=2, in=1, and=1, course=2, Devlabs=1, certification=1}
package com.dla; public class Palindrome { public static void main(String[] args) { String original = "nitin", reverse = ""; int length; length = original.length(); for (int i = length - 1; i >= 0; i--) { reverse = reverse + original.charAt(i); } if (original.equals(reverse)) System.out.println("The string is palindrome"); else System.out.println("The string is not a palindrome"); } }
Output: The string is palindrome
SDET is a highly skilled resource, having both development and testing skills, whereas manual testers have limited programming skills and can only prepare and execute the test cases.
SDET can develop test automation tools and can use it, but testers are not expected to develop the automation tools, they can only use the various automation tools.
INQUIRY
By tapping continuing, you agree to our Privacy Policy and Terms & Conditions
SkillAhead Solutions
Gurgaon
USA
1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA
`Copyright © DevLabs Alliance. All rights Reserved`
|
Refund & Reschedule Policy
Privacy Policy
Terms of Use