Coding Exercise - Anagram Checker You are given two strings str1 and str2. Your task is…
- User
Coding Exercise - Anagram Checker You are given two strings str1 and str2. Your task is to determine if str1 and str2 are anagrams of each other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word "listen" is an anagram of "silent", "hello" and "olelh" are anagrams.
You need to implement the areAnagrams(String str1, String str2) method of the StringMagic class, which returns true if str1 and str2 are anagrams of each other, and false otherwise.
Edge conditions:
The comparison should be case insensitive. That is, "Listen" and "Silent" should be considered anagrams.
If either str1 or str2 is null, the method should return false.
The method should also return false if the lengths of str1 and str2 are not the same.
Input format
The method takes in two parameters:
str1 - A String
str2 - A String
Output Format
The method should return:
true - If str1 and str2 are anagrams.
false - If str1 and str2 are not anagrams.
Instructions
To check whether two strings are anagrams:
Convert the strings to lowercase.
Check if the lengths of the strings are different. If so, return false.
Convert the strings to character arrays.
Sort the character arrays.
If the sorted arrays are equal, return true. Otherwise, return false.
Java Library Methods
Few methods would be helpful for you:
string.toLowerCase() - Converts all of the characters in this String to lower case.
string.toCharArray() - Converts this string to a new character array.
Arrays.sort(charArray) - Sorts the specified array into ascending numerical order.
Arrays.equals(charArray1, charArray2) - Returns true if the two specified arrays of chars are equal to one another.
These methods are very helpful for solving this problem, and you are encouraged to use them in your implementation.
- Assistantjava
import java.util.Arrays; public class StringMagic { public static boolean areAnagrams(String str1, String str2) { if (str1 == null || str2 == null) { return false; } str1 = str1.toLowerCase(); str2 = str2.toLowerCase(); if (str1.length() != str2.length()) { return false; } char[] charArray1 = str1.toCharArray(); char[] charArray2 = str2.toCharArray(); Arrays.sort(charArray1); Arrays.sort(charArray2); return Arrays.equals(charArray1, charArray2); } public static void main(String[] args) { System.out.println(areAnagrams("Listen", "Silent")); // true System.out.println(areAnagrams("hello", "olelh")); // true System.out.println(areAnagrams("Hello", "World")); // false } } - User
Coding Exercise - Is Hexadecimal String? In this problem, you are given a class MyString with a String variable str and two methods: isHexadecimalChar(char ch) and isHexadecimal().
You have to complete these methods.
A string is considered to be a valid hexadecimal if it only contains digits (0-9) and letters A-F (case-insensitive).
The isHexadecimalChar(char ch) method should return true if the character is a valid hexadecimal character (A-F or a-f) and false otherwise.
The isHexadecimal() method should return true if the string str is a valid hexadecimal string and false otherwise.
Edge Conditions
The isHexadecimal() method should return false for an empty string and for a null string.
The isHexadecimalChar(char ch) method should handle both upper and lower case characters.
Examples
"123F" => true "A23g" => false.
Notes
The Character.isDigit(char ch) method checks if a character is a digit.
The String.toCharArray() method converts a string to a character array.
The relational operators >= and <= are used to check if a character is within a certain range.
Be aware of the difference between lower case and upper case characters in ASCII. 'a'-'f' and 'A'-'F' are two distinct ranges.
- Assistantjava
class MyString { private String str; public MyString(String str) { this.str = str; } public boolean isHexadecimalChar(char ch) { return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'); } public boolean isHexadecimal() { if (str == null || str.isEmpty()) { return false; } for (char ch : str.toCharArray()) { if (!isHexadecimalChar(ch)) { return false; } } return true; } public static void main(String[] args) { MyString str1 = new MyString("123F"); System.out.println(str1.isHexadecimal()); // true MyString str2 = new MyString("A23g"); System.out.println(str2.isHexadecimal()); // false } } - User
Coding Exercise - Is Hexadecimal String? In this problem, you are given a class MyString with a String variable str and two methods: isHexadecimalChar(char ch) and isHexadecimal().
You have to complete these methods.
A string is considered to be a valid hexadecimal if it only contains digits (0-9) and letters A-F (case-insensitive).
The isHexadecimalChar(char ch) method should return true if the character is a valid hexadecimal character (A-F or a-f) and false otherwise.
The isHexadecimal() method should return true if the string str is a valid hexadecimal string and false otherwise.
Edge Conditions
The isHexadecimal() method should return false for an empty string and for a null string.
The isHexadecimalChar(char ch) method should handle both upper and lower case characters.
Examples
"123F" => true "A23g" => false.
Notes
The Character.isDigit(char ch) method checks if a character is a digit.
The String.toCharArray() method converts a string to a character array.
The relational operators >= and <= are used to check if a character is within a certain range.
Be aware of the difference between lower case and upper case characters in ASCII. 'a'-'f' and 'A'-'F' are two distinct ranges.
- Assistantjava
class MyString { private String str; public MyString(String str) { this.str = str; } public boolean isHexadecimalChar(char ch) { return Character.isDigit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'); } public boolean isHexadecimal() { if (str == null || str.isEmpty()) { return false; } for (char ch : str.toCharArray()) { if (!isHexadecimalChar(ch)) { return false; } } return true; } public static void main(String[] args) { MyString str1 = new MyString("123F"); System.out.println(str1.isHexadecimal()); // true MyString str2 = new MyString("A23g"); System.out.println(str2.isHexadecimal()); // false } } - User
Coding Exercise - Word Reverser In this exercise, you are required to write a Java method called reverseWordsInSentence within the provided StringMagic class.
The method should take a string sentence as input and return a string in which each word in the sentence is reversed while maintaining the original word order.
Edge conditions:
If the input string is null, your method should return the string "INVALID".
If the input string is empty (i.e., ""), your method should return an empty string.
Input Format
A single string which is a sentence.
Output Format
A string in which each word in the sentence is reversed while maintaining the original word order.
Examples
Let's take a look at some examples:
Example 1
Input:
StringMagic sm = new StringMagic(); sm.reverseWordsInSentence("Hello World"); Return Value:
- Assistantjava
public class StringMagic { public String reverseWordsInSentence(String sentence) { if (sentence == null) { return "INVALID"; } if (sentence.isEmpty()) { return ""; } String[] words = sentence.split(" "); StringBuilder reversedSentence = new StringBuilder(); for (String word : words) { StringBuilder reversedWord = new StringBuilder(word).reverse(); reversedSentence.append(reversedWord).append(" "); } // Trim to remove the extra space added at the end return reversedSentence.toString().trim(); } public static void main(String[] args) { StringMagic sm = new StringMagic(); System.out.println(sm.reverseWordsInSentence("Hello World")); // "olleH dlroW" System.out.println(sm.reverseWordsInSentence("The quick brown fox")); // "ehT kciuq nworb xof" System.out.println(sm.reverseWordsInSentence("")); // "" System.out.println(sm.reverseWordsInSentence(null)); // "INVALID" } }