Posts

Showing posts from 2020

Program to print the duplicate characters in a string

public static ArrayList<String> checkForDuplicates(String enteredString) { char[] data = enteredString.toCharArray(); ArrayList<String> duplicateChars = null; for(int i = 0; i < enteredString.length(); i++) { for(int j = i+1; j < enteredString.length(); j++) { if(data[i] == (data[j])) { System.out.println("Duplicate Characters "+data[j]); break; } } } return duplicateChars; } public static void main(String[] args) { checkForDuplicates("madam"); }

Program to check if entered string is Palindrome in Java

 public static boolean checkForPalindrome(String reverseString) { StringBuffer reversedString = new StringBuffer(); char[] reverseArray = reverseString.toCharArray(); for(int i = reverseArray.length-1; i>=0; i--) { reversedString.append(reverseArray[i]); } if(reverseString.equals(reversedString.toString())) { return true; } else { return false; } } public static void main(String[] args) { boolean reversedString = checkForPalindrome("MADAM"); if(reversedString) { System.out.println("Entered string is a Palindrome"); } else { System.out.println("Entered string is not a Palindrome"); } }

Program to Reverse a string in Java

public static String reverseString(String reverseString) { StringBuffer reversedString = new StringBuffer(); char[] reverseArray = reverseString.toCharArray(); for(int i = reverseArray.length-1; i>=0; i--) { reversedString.append(reverseArray[i]); } return reversedString.toString(); } public static void main(String[] args) { String reversedString = reverseString("Keshav"); System.out.println("Reversed String "+reversedString); }