quest 1.) checkPalindrome: Please write the function boolean checkPalindrome(Str
ID: 3809547 • Letter: Q
Question
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
Question 1:
public class Demo {
boolean checkPalindrome(String str)
{
String original=str;String reverse = "";
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
{
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d=new Demo();
System.out.println("string bcbe returns from checkPalindrome "+d.checkPalindrome("bcbe"));
}
}
====================================================
Output:
string bcbe returns from checkPalindrome false
============================================================================
Question 2:
public class Demo {
public boolean checkAnagram(String str1, String str2)
{
if(str1.length() != str2.length())
{ return false; }
char[] chars = str1.toCharArray();
for(char c : chars)
{ int index = str2.indexOf(c);
if(index != -1)
{
str2 = str2.substring(0,index) + str2.substring(index +1, str2.length());
}
else
{
return false;
}
}
return str2.isEmpty();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d=new Demo();
System.out.println("string parliament and partialmen returns from checkAnagram "+d.checkAnagram("parliament","partialmen"));
}
}
==========================================================
Output:
string parliament and partialmen returns from checkAnagram true
===================================================================
Question 3:
public class Demo {
public String reverseStringInWord(String s) {
if (s == null || s.length() == 0) {
return "";
}
String[] arr = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = arr.length - 1; i >= 0; --i) {
if (!arr[i].equals("")) {
sb.append(arr[i]).append(" ");
}
}
return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d=new Demo();
System.out.println("string I am good returns from reverseStringInWord "+d.reverseStringInWord("I am good"));
}
}
===============================================================
output:
string I am good returns from reverseStringInWord good am I
================================================================
Question 5:
public class Demo {
private static char[] ReverseStringInGroup(char[] a, int k) {
int i;
int rem = a.length % k; //to fig out if any elements will be left at the end
for(i = 0; i < a.length ; i = i + k) {
a = reverseArray(a, i, i+k-1);
}
if(rem != 0) {
reverseArray(a, a.length - rem, a.length - 1);
}
return a;
}
private static char[] reverseArray(char[] a, int start, int end) {
char temp;int i ,j;
char [] buf=a;
for(i = start, j = end; i < j && j < a.length ; i++, j--) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
return buf;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d=new Demo();
char [] a="abcdef".toCharArray();
System.out.println("abcdef returns from function ReverseStringInGroup with k=2" );
System.out.println(d.ReverseStringInGroup(a, 2));
}
}
===============================================================
Output:
abcdef returns from function ReverseStringInGroup with k=2
badcfe
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.