The document contains Java code solutions for various string manipulation problems, including finding the first unique character, checking for anagrams, reversing words in a sentence, finding the longest palindrome, calculating the digit sum, and sorting words by length. Each problem is accompanied by a brief description, input examples, and corresponding output. The code is structured in a class format with main methods for execution.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
day2StringTask
The document contains Java code solutions for various string manipulation problems, including finding the first unique character, checking for anagrams, reversing words in a sentence, finding the longest palindrome, calculating the digit sum, and sorting words by length. Each problem is accompanied by a brief description, input examples, and corresponding output. The code is structured in a class format with main methods for execution.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
Q1)Print the first unique character from a given string.
input as: abbacdeab
Output as: c Ans: public class MainClass{ public static char printFirstUniqueCharacters(String s){ char[] ch = s.toCharArray(); for(int i=0; i<ch.length; i++) { int j=0; for(; j<ch.length; j++) if(i!=j && ch[i]==ch[j]) break; if(j == ch.length) return ch[i]; } return '-'; } public static void main(String []args){ String s = "Developer"; System.out.println("First unique character: " + printFirstUniqueCharacters(s)); } } =================================================================================== ================ Q2)Check the String is Anagram or not. String s1 = "care"; String s2 = "race"; Output : Strings are anagram Ans: import java.util.Arrays; public class MainClass{ public static boolean isAnagram(String s1, String s2){ if(s1.length() == s2.length()){ char []c1 = s1.toCharArray(); char []c2 = s2.toCharArray(); Arrays.sort(c1); Arrays.sort(c2); int i=0; for(; i<c1.length; i++) if(c1[i]!=c2[i]) return false; if(i == c1.length) return true; } return false; } public static void main(String []args){ String s1 = "care"; String s2 = "race"; System.out.println("Strings is anagram: " + isAnagram(s1, s2)); } } =================================================================================== ================ Q3)Reverse each word of a given sentence at their place. Input as : java is a platform Output as: avaj si a mroftalp Ans: public class MainClass{ public static String reverseEachWord(String s){ String []sa = s.split("\\s"); String res = ""; for(int i=0; i<sa.length; i++){ char []ch = sa[i].toCharArray(); for(int j=0; j<ch.length/2; j++){ char t = ch[j]; ch[j] = ch[ch.length-j-1]; ch[ch.length-j-1] = t; } res += " " + String.valueOf(ch); } return res; } public static void main(String []args){ String s = "java is a platform"; System.out.println("After reversed each word: " + reverseEachWord(s)); } } =================================================================================== ================ Q4) Find the longest palindrome in a given String. Input as : nitin is learning malayalam from madam liril. Output as: Malayalam Ans: public class MainClass{ public static String findLargestPalindrome(String s){ int max = 0; String largestPalindeome = ""; String []sa = s.split("\\s"); for(int i=0; i<sa.length; i++){ int j=0; char []t = sa[i].toCharArray(); for(; j<t.length/2; j++) if(t[j] != t[t.length-j-1]) break; if(j == t.length/2) if(max < t.length){ max = t.length; largestPalindeome = sa[i]; } } return largestPalindeome; } public static void main(String []args){ String s = "nitin is learning malayalam from madam liril"; System.out.println("Largest palindrome: " + findLargestPalindrome(s)); } } =================================================================================== ================ Q5) Find the digit sum from the given String. Input as: @g123mai5l1.c2om Output as: 14 Ans: public class MainClass{ public static int sumOfDigitsInString(String s){ int sum = 0; for(int i=0; i<s.length(); i++) { if(Character.isDigit(s.charAt(i))) sum += s.charAt(i) - 48; } return sum; } public static void main(String []args){ String s = "@g123mai5l1.c2om"; System.out.println("Sum of digits of given string: " + sumOfDigitsInString(s)); } } =================================================================================== ================ Q6) Sort the given String based on their word length. Input as : India is a great nation. Output as : a is India great nation Ans: public class MainClass{ public static String sortBasedOnWordLength(String s){ String []sa = s.split("\\s"); String res = ""; for(int i=0; i<sa.length; i++){ for(int j=i+1; j<sa.length; j++) if(sa[i].length() > sa[j].length()){ String t = sa[i]; sa[i] = sa[j]; sa[j] = t; } res += sa[i] + " "; } return res; } public static void main(String []args){ String s = "India is a great nation"; System.out.println("After sorting based on word length: " + sortBasedOnWordLength(s)); } }