The user then gets to choose options from the following menu: 1. Remove adjacent duplicate characters appearing in the input string, and report the number of characters removed Create a new linked list comprising of only the vowels used in the input string and in the exact order. Display the contents of this 2. linked list 3. Delete all occurrences of a character: Please enter the character 4. Search for a substring in the input string: Please enter the substring 5. Display the linked list in reverse 6. Count of occurrences of a character: Please enter a character 7. Display the current content of the linked list 8. Exit Here are some further clarifications on some of the menu options: Option 1. For example we would like to change aarppexxx to become arpex. Option 2. For example beautiful should give us a new linked list containing eauiu. Option 4. Searching for the substring iss in mississippi must display the following message: The substring iss is found 2 time (s) in the input string. Option 7. This option will display the current content of the linked list reflecting possible changes as a result of menu options chosen by the user.
in option 4 i want it to tell also which string is coming double it should show it
Option 4. Searching for the substring iss in mississippi must display the following
message:
The substring iss is found 2 time(s) in the input string.
Option 7. This option will display the current content of the linked list reflecting possible
changes as a result of menu options chosen by the user.
you can see picture for help
package javaapplication2;
import java.util.*;
public class WordOp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word in lowercase letters :");
String word = sc.nextLine();
char c ;
String temp;
int count;
LinkedList<Character> linkedlist= new LinkedList<>();
while(true)
{
//ask the user for the choice of operation
System.out.print("\n*******Word Operations******\n1.Remove adjacent dupicate character\n2.create LinkedList Vowels\n3.delete occurence of character\n4.search substring\n5.Display the linked list in revers\n6.count occurrence of the character\n7.Diplays the content of the linked list\n8.Exit\n.Enter your choice:");
int choice = sc.nextInt();
// use a switch to execute statement of the respective choice
switch(choice)
{
case 1 : c= word.charAt(0);
temp = c+"";
// variable to maintain the count of removed characters
count = 0 ;
for (int i=1 ; i<word.length() ; i++)
{
// if the current character is not same as previous then add it to temp
if(word.charAt(i)!=c)
{
temp=temp+word.charAt(i);
c=word.charAt(i);
}
else
count++;
}
word = temp;
System.out.println("Word: "+ word);
System.out.println("Number of characters removed: "+count);
break ;
case 2 : linkedlist = new LinkedList<>();
String Vowels="aeiou";
for(int i=0 ; i<word.length(); i++)
{
// check if the character is contained in the vowels string
if(Vowels.contains(word.charAt(i)+""))
linkedlist.add(word.charAt(i));
}
System.out.print("Elements of linkedlist:");
for(char ch: linkedlist)
System.out.print(ch);
System.out.println();
break;
case 3 : System.out.print("Enter the character:");
c=sc.next().charAt(0);
temp="";
for(int i=0 ; i<word.length(); i++)
{
// if the character in not specified character then it add to temp
if(word.charAt(i)!=c)
temp=temp+word.charAt(i);
}
word=temp;
System.out.println("Word: "+word);
break;
case 4 : System.out.print("Enter the substring;" );
// after entering the choice, we press enter button which will generate \n character.
//this will be scanned by nextline() method which will give us a blank input hence
//we need to absorb this \n character with the below line
sc.nextLine();
//after that we will scan the nextLine
temp= sc.nextLine();
// use contains() method of string class to check if the substring is present in the word
//it will return true if the specified string is present in the word
if (word.contains(temp))
System.out.println("the substring is present");
else
System.out.println("The substring is not present");
break;
case 5 : // use temp to generate the linkedlist in reverse order
temp="";
System.out.print("Elements of linked list in reverse:");
//loop through each character present in the linkedlist
for(char ch: linkedlist)
temp=ch+temp; //contact the character in front of the temp string each time
System.out.println(temp);
break;
case 6 : System.out.print("Enter the character:");
c=sc.next().charAt(0);
count=0;
//use a for loop and increment count if the current character is specified character
for(int i=0; i<word.length(); i++)
count++;
System.out.println("occurrence:+"+count);
break;
case 7 : System.out.print("Elements of linkedlist:");
for (char ch:linkedlist)
System.out.print(ch);
System.out.println();
break;
case 8 : // use exit () to close the program
System.exit(0);
default : System.out.println("Enter a valid choice");
}
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps