Must-Know Programs for Campus Recruiments: Part 3

Write a program to copy a string
public class CopyString{ public static void main(String[] args) { String str = "Hello"; String str2 = new String(str); System.out.println(str2); } }Write a program to concatenate a string
public class ConcatString{ public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String concatenated = str1.concat(str2); System.out.println("Concatenated String: " + concatenated); } }Write a program to reverse a string.
public class ReverseString{ public static void main(String[] args){ String str = "Hello"; String reversed = new StringBuilder(str).reverse().toString(); System.out.println("Reversed String: "+ reversed); } }Write a program to print a string.
public class PrintString{ public static void main(String[] args){ String str = "Hello World"; System.out.println("Printed String: " + str); } }Write a program to print the length of a string.
public class LengthString{ public static void main(String[] args) { String str = "Hello"; System.out.println("Length of String: " + str.length()); } }Write a program to compare two strings.
public class CompareStrings { public static void main(String[] args){ String str1 = "Hello"; String str2 = "Hello"; System.out.println("Strings are equal: " + str1.equals(Str2)); } }Write a program to print length of the string without using strlen() function.
public class LengthString{ public static void main(String[] args) { String str = "Hello"; int length = 0; for(char c : str.toCharArray()){ length++; } System.out.println("Length without strlen: " + length); } }Write a program to toggle each character in a string.
public class ToggleString { public static void main(String[] args) { String str = "HeLLo"; char[] charArray = str.toCharArray(); for(int i = 0; i < charArray.length; i++) { if(charArray[i] >= 'A' && charArray[i] <='Z'){ charArray[i] = (char) (charArray[i] + 32); // convert to lowercase } else if(charArray[i] >= 'a' && charArray[i] <= 'z'){ charArray[i] = (char) (charArray[i] - 32); // convert to uppercase } } String toggled = new String(charArray); System.out.println("Toggled String: " + toggled); } }Write a program to remove vowels from a string.
public class RemoveVowels { public static void main(String[] args) { String str = "Hello"; char[] charArray = str.toCharArray(); StringBuilder result = new StringBuilder(); for(int i = 0; i < charArray.lnegth; i++) { char c = charArray[i]; if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U') { result.append(c); } } String noVowels = result.toString(); System.out.println("String without vowels: " + noVowels); } }Write a program to check if string is palindrome or not.
public class PalindromeCheck { public static void main(String[] args) { String str = "Hello"; String reversed = new StringBuilder(str).reversed().toString(); if(str == reversed) { System.out.println("the given string is palindrome"); } else { System.out.println("The given string is not palindrome"); } } }Write a program to sort a string in alphabetical order.
public class KeepAlphabets { public static void main(String[] args) { String str = "Hello123!"; String onlyAlphabets = str.replaceAll("[^a-zA-Z". ""); System.out.println("Only alphabets: " + onlyAlphabets); } }Write a program to Remove brackets from an algebraic expression.
public class RemoveBrackets { public static void main(String[] args) { String str = "a + b + (c - d)"; String noBrackets = str.replaceAll("[()]",""); System.out.println("Expression without brackets: " + noBrackets); } }Write a program to remove spaces from a string.
public class RemoveSpaces { public static void main(String[] args) { String str = "Hello World" String noSpaces = str.replaceAll("\\s",""); System.out.println("String without spaces: " + noSpaces); } }Write a program to count the sum of numbers in a string.
public class SumNumbers { public static void main(String[] args) { String str = "I have 5 apples and 3 grapes"; int sum = 0; java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("\\d+").matcher(Str); while(matcher.find()){ sum += Integer.parseInt(matcher..group()); } System.out.println("Sum of numbers: " + sum); } }Write a program to Capitalize the first and last letter of each word of a string.
public class CapitalizeWords{ public static void main(String[] args){ String str = "hello world"; String[] words = str.split(" "); StringBuilder capitalized = new StringBuilder(); for(String word : words) { if(word.length() > 0) { capitalized.append(Character.toUpperCase(word.charAt(0))); .append(word.substring(1,word.length() - 1)) .append(Character.toUpperCase(word.chartAt(word.length() - 1))); .append(" "); } } System.out.println("Capitalised: " + capitalized.toString().trim()); } }Write a program to calculate the frequency of character in a string.
public class CharFrequency { public static void main(String[] args) { String str = "hello"; java.util.Map<Character, Integer> freq = new java.util.HashMap<>(); for(char c : str.toCharArray()){ freq.put(c, freq.getOrDefault(c,0) + 1; } System.out.println("Character frequency: " + freq); } }Write a program to print non repeating characters in a string.
public class NonRepeatingChars{ public static void main(String[] args) { String str = "hello"; java.util.Set<Character> seen = new java.util.HashSet<>(); java.util.Set<Character> nonRepeating = new java.util.HashSet<>(); for(char c : str.toCharArray()){ if(!seen.add(c)){ nonRepeating.remove(c); } else{ nonRepeating.add(c); } } System.out.println("Non-repeating characters: " + nonRepeating); } }Write a program to check if two strings are Anagram or not.
public class AnagramCheck{ public static void main(String[] args){ String str1 = "listen"; String str2 = "silent"; char[] charArray1 = str1.replaceAll("[\\s]", "").toLowerCase().toCharArray(); char[] charArray2 = str2.replaceAll("[\\s]", "").toLowerCase().toCharArray(); java.util.Arrays.sort(charArray1); java.util.Arrays.sort(charArray2); boolean isAnagram = java.util.Arrays.equals(charArray1, charArray2); System.out.println("Are Anagram: " + isAnagram); } }Write a program to Replace substring in a string.
public class ReplaceSubstring{ public static void main(String[] args) { String str = "Hello World"; String replaced = str.replace("World","Java"); System.out.println("Replaced String: " + replaced); } }Write a program to count common subsequence in two strings.
public class CommonSubsequence { public static void main(String[] args) { String str1 = "ABCDGH"; String str2 = "AEDFHR"; int m = str1.length(); int n = str2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } System.out.println("Length of common subsequence: " + dp[m][n]); } }


