In: Computer Science
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)
//----- 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.