Question

In: Computer Science

Public static boolean isPalindrome(String word) * Check to see if a word is a palindrome. Should...

Public static boolean isPalindrome(String word)
* Check to see if a word is a palindrome. Should be case-independent.
* @param word a String without whitespace
* @return true if the word is a palindrome
  

Public static int longestWordLength(String words)

* Returns length of the longest word in the given String using recursion (no loops).
* Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space,
* use the following method on your String to remove punctuation {@code .replaceAll("[^a-zA-Z]", "")}
* If you use a Scanner, you will need a helper method to do the recursion on the Scanner object.
* @param words A String containing one or more words.
* @return The length of the longest word in the String.
* @see Scanner#Scanner(String)
* @see Scanner#next()
* @see String#split(String)
* @see String#replaceAll(String, String)
* @see Math#max(int, int)

Solutions

Expert Solution

//----- Operations.java ---------
import java.util.Scanner;
class Operations
{
   //function that returns a boolean whether passed word is palindrome
   //or not, case independent means abbA is a palindrome.
   public static boolean isPalindrome(String word)
   {
       //first assume given word is palindrome
       boolean isPal = true;
       //convert the string into lower case;
       word = word.toLowerCase();
       //cal length
       int len = word.length();
       //now iterate from 0 to len/2(inclusive)
       for(int i =0;i<=len/2;i++)
       {
           //compare first char with last char, second char
           //with second last char and so on..
           if(word.charAt(i) != word.charAt(len - i -1))
           {
               //whenever they are not equal then put isPal to false and break
               isPal = false;
               break;
           }
       }
       //return isPal
       return isPal;
   }
   //function that returns the longest word length in given parameter words
   public static int longestWordLength(String words)
   {
       //first replace all non alphabets except space with ''
       words = words.replaceAll("[^a-zA-Z ]", "");
       //now call the longestWordRecursion that passes current words sentence
       //and current longest word which is currently is an empty string.
       return longestWordRecursion(words,"").length();
   }
   //function that recursively finds the longes word and returns it.
   public static String longestWordRecursion(String words,String longestWord)
   {
       //find the space index
       int ind = words.indexOf(' ');
       //if no space is found
       if(ind == -1)
       {
           //return the largest among current words value and the longestWord
           return longestWord.length() < words.length() ? words: longestWord;
       }
       //update the longestWord value.
       //if the index found which is the length of the first word in the current words variable separated by space.
       //is greather than the previously longest Word
       //update it if not don't update
       longestWord = ind > longestWord.length() ? words.substring(0,ind) : longestWord;
       //update the words variable by storing the sentence by removing current traversed word
       words = words.substring(ind+1);
       //call the function by update values of words and longestWord
       return longestWordRecursion(words,longestWord);
   }
   //main method
   public static void main(String[] args)
   {
       //word to test palindrom
       String word = "abba";
       System.out.println("\nIs Word: "+word+" palindrome: "+isPalindrome(word));
      
       //words to test longest word.
       String words = "i love my country";
       System.out.println("\nSentence: "+words);
       System.out.println("Longest Word: "+longestWordLength(words));
   }

}

//if you have any doubts please comment and i will fix.

//PLEASE LIKE THE ANSWER.


Related Solutions

Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
I wrote this program to check a string is palindrome or not. but in both cases,...
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome. could someone help me with this program? it has an error I couldn't find. when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome. #include<string> #include <iostream> using namespace std; class PString:public string...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Required methods:: public static void processAccount (Account[] acc, printWriter out{}. public static boolean deposit(Account a){}.
Java programming. Required methods::public static void processAccount (Account[] acc, printWriter out{}.public static boolean deposit(Account a){}.public static boolean withdrawal(Account a){}.public static int findAccount(long accountNumber, Account[] acc){}. 1/ Remove the applyRandomBonus method from the test class2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file:a. Skip any lines that cause an exceptionb. Write information about the exception to the log file, yourlastnameError.txtc. Include exception type and line number in exception message...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT