Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java quest 1.) checkPalindrome: Please write the function boolean checkPalindrom

ID: 3809550 • Letter: J

Question

java

quest 1.) checkPalindrome: Please write the function boolean checkPalindrome(String str)to test if a given string is palindrome or not. (Note: please consider alphabet character only and case insensitive.)

Example 1: Input: “ab,B* a”; Return: True Example 2: Input: “bcbe”; Return False

quest 2: checkAnagram: Please write a function boolean checkAnagram (String str1, String str2) to check if str1 is an anagram of str2. (Note: an anagram is a word or a phrase made by transposing the letters of another word or phrase. Please consider alphabet character only and case insensitive.)

Example 1: Input: (“parliament”, “partial men”); Return: True Example 2: Input: (“hello”, “hell god”); Return False

quest 3: reverseStringInWord: Please fill the function String reverseStringInWord(String str) to return a string which reverse the input word by word. (Note: the words are separated by one more whitespace.)

Example 1: Input: (“I am good”); Return: good am I Example 2: Input: (“How are you”) Return you are How

quest 4:  MagicString: Please fill the function boolean magicString(String str) to check if a string str is a magic string. A magic String is the one which can be constructed by repeating the substring of it. (Note: you can assume all the characters are in lowercase).

Example 1: Input: abcabcabc Return: True Example 2: Input: ababc Return False

quest 5: ReverseStringInGroup: Please fill the function String reverseInGroup(String str, int k) to reverse the given string in group with size as k.

Example 1: Input: str = “abcdef”, k = 2 Return: badcfe Example 2: Input: str = “abcdef”, k = 4 Return dcbafe

quest 6: checkCapital: Please fill the function boolean checkCapital(String str) to check if the given string if a Capital String. A String is called a Capital String only if one of the following conditions is satisfied: a) All the characters in the String are capitals b) Only the first character is capital and the string has more than characters.

Example 1: Input: str = “Computer” Return: True Example 2: Input: str = “GOOD” Return True Example 3: Input: str = “AmericA” Return False

Explanation / Answer


public class CheckPalindromeClass {

   boolean checkPalindrome(String str)
   {
       // ab,B*a true
       // bcbe false
       str = str.replaceAll("\W", ""); // used to replace all junk special characters
       str = str.toLowerCase();
       int len = str.length();
       for (int index = 0; index <len; ++index)
       {
       if (str.charAt(index) != str.charAt(len - index - 1))
       {
       return false;
       }
       }
       return true;
   }
  
  
  
   boolean checkAnagram(String str1, String str2)
   {
       str1 = str1.replaceAll("\s+",""); // used to remove white spaces from string to check anagrams
       str2 = str2.replaceAll("\s+",""); // used to remove white spaces from string to check anagrams
       System.out.println(str1 +" & " +str2);
      
       char[] chars = str1.toCharArray(); // make character array from string
       StringBuilder second_string = new StringBuilder(str2);
       for(char ch : chars)
       {
           int index = second_string.indexOf("" + ch);
           if(index != -1)
               second_string.deleteCharAt(index);
           else
               return false;
       }
       return second_string.length()==0?true:false;
   }

   // split the string to word array
   String reverseStringInWord(String str)
   {
       if(str == null || str.length()==0)
           return "";
      
       // split to words
       String[] char_array = str.split(" ");      
       StringBuilder sbuilder = new StringBuilder();
      
       for (int index = char_array.length - 1; index >= 0; --index) {
           if (!char_array[index].equals("")) {
               sbuilder.append(char_array[index]).append(" ");
           }
       }
      
       if(sbuilder.length() == 0)
           return "";
       else
           return sbuilder.substring(0, sbuilder.length() - 1);
   }
  
  
  
   boolean checkCapital(String str)
   {
       boolean check_all = false;
      if(!Character.isUpperCase(str.charAt(0))) // if first char is non upper
          return false;
     
      if(Character.isUpperCase(str.charAt(1))) // if second char is upper then all should be capital to pass
          check_all = true;
  
     
      if(check_all == true)     
      {
          for (int index=2; index<str.length(); index++)
       {
              if(!Character.isUpperCase(str.charAt(index)))
                  return false;
       }
      }
     
      if(check_all == false) // if second character is not upper, so all should be small
       {
           for (int index=2; index<str.length(); index++)
           {
               if(Character.isUpperCase(str.charAt(index)))
                      return false;
           }
       }
      return true;
   }
  
   public static void main(String args[])
   {
       CheckPalindromeClass obj = new CheckPalindromeClass();
       String str = "ab,B*a";
       System.out.println(str);
       System.out.println(obj.checkPalindrome(str));
       str = "Ba,B*a**.B";
       System.out.println(str);
       System.out.println(obj.checkPalindrome(str));
       str = "Ba,B*a**.Ba";
       System.out.println(str);
       System.out.println(obj.checkPalindrome(str));
      
       String str1 = "parliament";
       String str2 = "partial men";
       System.out.println(str1 +" & " +str2);
       System.out.println(obj.checkAnagram(str1, str2));
       str1 = "hello";
       str2 = "lelho";
       System.out.println(str1 +" & " +str2);
       System.out.println(obj.checkAnagram(str1, str2));
       str1 = "hello";
       str2 = "hell god";
       System.out.println(str1 +" & " +str2);
       System.out.println(obj.checkAnagram(str1, str2));

      
       str1 = "I am Good";
       System.out.println(str1);
       System.out.println(obj.reverseStringInWord(str1));
       str1 = "How are You";
       System.out.println(str1);
       System.out.println(obj.reverseStringInWord(str1));
      
       str1 = "Computer";
       System.out.println(str1);
       System.out.println(obj.checkCapital(str1));
       str1 = "GOOD";
       System.out.println(str1);
       System.out.println(obj.checkCapital(str1));
       str1 = "AmericA";
       System.out.println(str1);
       System.out.println(obj.checkCapital(str1));

   }
}