Question

Create the following JAVA method: the utility will reverse the characters in each word, where a...

Create the following JAVA method:

the utility will reverse the characters in each word, where a word is a sequence of characters terminated by either a whitespace, if no delimiters are specified, or any single character in the string of delimiters, otherwise. Note that the end of the file always ends a word.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.util.Scanner;

  
class reverWords {

   static void wordRevese(String sentence)
   {
       //declare a character stack
Stack<Character> myStack=new Stack<Character>();

// Read till a space is found for the word
for (int i = 0; i < sentence.length(); ++i) {
if (sentence.charAt(i) != ' ')
   myStack.push(sentence.charAt(i));
//when space found
else {
while (myStack.empty() == false) {
System.out.print(myStack.pop());
}
System.out.print(" ");
}
}
// handling last word as it may not have a space at the end
while (myStack.empty() == false) {
System.out.print(myStack.pop());
  
}
}
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String sentence = sc.nextLine();

wordRevese(sentence);
sc.close();
}
}

Add a comment
Know the answer?
Add Answer to:
Create the following JAVA method: the utility will reverse the characters in each word, where a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to create a program called Count.java that contains a single method called compute...

    I am trying to create a program called Count.java that contains a single method called compute with a string parameter called value. The method should count the number of newlines, words (any sequence of characters separated with whitespace), and characters in a given string and return an array of the three integer values. Use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

  • Java plz You, as the author, discovered a particular word (a sequence of non-whitespace characters) in...

    Java plz You, as the author, discovered a particular word (a sequence of non-whitespace characters) in your paper was misspelled, and you would like to correct the error. Implement the following method declaration to replace all occurrences of wrongWord with newWord in the array words: public static void replace(String[] words, String wrongWord, String newWord)

  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • [Java] Create a class called FileExercises that contains the following method Method encrypt that does not...

    [Java] Create a class called FileExercises that contains the following method Method encrypt that does not return anything and takes a shift, input filename and output filename as arguments. It should read the input file as a text file and encrypt the content using a Caesar cypher with the shift provided. The resulting text should be placed in the output file specified. (If the output file already exists, replace the existing file with a new file that contains the required...

  • [Java] I need help completing the method below. The instructions are also below. public int wordSearch(String...

    [Java] I need help completing the method below. The instructions are also below. public int wordSearch(String word, String filename) { } - find the first time the word appears in a text file filename and returns the position of the word in the file(if its the first word in the file, method should return 1) - assume no punctuation in file - assume all whitespace is either a new line or single space - do not include the times that...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • Write a function that, given a word and a set of characters, filters all occurrences of...

    Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT