Question

In: Computer Science

An alliteration is a stylistic device in which successive words begin with the same letter (or...

An alliteration is a stylistic device in which successive words begin with the same letter (or sometimes the same sound). In our case we will only consider alliterations that follow the "same letter" property. It is often the case that short words (three letters or less) are inserted to make the sentence make sense. Here are some examples of alliterations:

  • alliteration's artful aid

  • Peter parker's poems are pretty

  • Round the rugged rock the ragged rascal ran

  • Peter Piper picked a peck of pickled peppers

Your program will ignore words that are three letters or less when searching for alliterations. For this program, you may assume that the first word of the sentence is at least 4 letters long. For this part of your lab, you will write a java program that asks the user to enter a sentence, word or phrase, and respond by telling the user if the input is an alliteration.

An example of your program's interactions are shown below:

ALLITERATION DETECTOR

ease enter a sentence: Peter parker's poems are pretty

Your input was: "Peter parker's poems are pretty"

The input is an alliteration

ALLITERATION DETECTOR

Please enter a sentence: Able was I ere I saw Elba

Your input was: "Able was I ere I saw Elba"

The input is not an alliteration

Solutions

Expert Solution

The approach to solve the problem is as follows:

  • Read the text input.
  • Store the first letter of the first word.
  • Split the input into string array of words.
  • Now set a bool value of alliteration as true.
  • Use a loop and compare the letter with first letter of each word.
  • If the letter does not match and the size of word is four or greater, set the bool value to false and break from the loop.
  • If the bool value is still true, the text contains alliteration, otherwise it does not contain alliteration.

Java program:

import java.util.Scanner;

class Main{
    public static void main(String args[]){
        
        //object to input from keyboard
        Scanner sc = new Scanner(System.in);
        
        System.out.println("ALLITERATION DETECTOR");
        
        //input string from user
        System.out.print("Please enter a sentence:");
        
        String text = sc.nextLine();
        
        //display the input
        System.out.println("Your input was:" + text);
        
        //the first letter of first word
        char check = text.charAt(0);
        
        //set alliteration to true
        boolean alliteration = true;
        
        //split the string into array
        String data[] = text.split(" ");
        
        //traverse all words
        for(int i=0;i<data.length;i++){
            
            //if the letter does not match and the word length is 4 or greater
            //set alliteration to false and break
            if (data[i].charAt(0) != check && data[i].length() >= 4 )
            {
            alliteration = false;
            break;
            }
        }
        
        //if alliteration is true
        if(alliteration){
        System.out.println("The input is an alliteration. ");
        }
        
        //alliteration is false
        else {
         System.out.println("The input is not an alliteration. ");
        }
    }
}

Snippet of code


Related Solutions

C++ Add a function that finds & displays all the words that begin with the letter...
C++ Add a function that finds & displays all the words that begin with the letter 's'. The text file "dickens.txt" is as follows: It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was...
You are entering a letter ‘B’ from an input device to be displayed on a monitor...
You are entering a letter ‘B’ from an input device to be displayed on a monitor via an 8-bit microcontroller. Describe the steps/processes of the 8-bit microcontroller needs to perform to display the output on the monitor.
If I want to know whether successive cohorts show the same pattern of decline in frequency...
If I want to know whether successive cohorts show the same pattern of decline in frequency of close friendships in their 30s, which research design should I use??
Given L={words in which the number of a’s are the same as the number of b’s}...
Given L={words in which the number of a’s are the same as the number of b’s} Write a CFG to accept L. Convert the CFG to CNF. Construct a PDA for this CNF.
Two soccer players, Mary and Jane, begin running from nearly the same point at the same...
Two soccer players, Mary and Jane, begin running from nearly the same point at the same time. Mary runs in an easterly direction at 4.34 m/s, while Jane takes off in a direction 60.9o north of east at 5.71 m/s. How long is it before they are 26.7 m apart? Tries 0/8 What is the velocity of Jane relative to Mary? Enter first the x-component and then the y-component. Tries 0/8 How far apart are they after 3.96 s?
2. How many seven – letter code words can be formed using a standard 26 letter...
2. How many seven – letter code words can be formed using a standard 26 letter alphabet (12 points) a.         if repetition is allowed? b.         if repetition is not allowed? c.         if the first letter has to be a C and the rest of the letters are different? d.         if the first two letters have to be a vowel and the rest of the letters are different?
Cover Letter: 250-300 words, formatted as a business letter, related to Accounting/ Finance position. Research an...
Cover Letter: 250-300 words, formatted as a business letter, related to Accounting/ Finance position. Research an organization you would like to work for in the future. Explore their background, values, and culture. Then determine what they look for in prospective employees (you may use a recent or current job ad for reference). Write a cover letter expressing your interest in working for your chosen organization. Please attached the job ad if you use one.
Describe (words and diagrams) Kepler’s method of successive maximum elongations for finding the orbit shape of...
Describe (words and diagrams) Kepler’s method of successive maximum elongations for finding the orbit shape of Mercury.
Is paying men more than women for the same job considered a rationing device?
Is paying men more than women for the same job considered a rationing device?
QUESTION 20 Floating-point instruction names begin with the letter F to distinguish them from CPU instructions....
QUESTION 20 Floating-point instruction names begin with the letter F to distinguish them from CPU instructions. True False 1 points    QUESTION 21 Which of the following exception conditions are recognized and detected by the floating point unit (FPU)? a. Inexact precision b. Divide by zero c. Numeric overflow d. Numeric underflow 1 points    QUESTION 22 How many operands can a floating-point instruction have? a. Can have zero operands. b. Can have two operands. c. Can have one immediate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT