Question

In: Computer Science

So I need to make a java program that reverse or replace first or lastchar or...

So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input.

length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use

for example

if user put asdf asdf and chose reverse

input should be fdsa fdsa

if user put asdf asdf and chose replace first a with b

input should be bsdf asdf

if user put asdf asdf and chose remove a and 2

input should be asdf sdf (remove 2nd 'a')

aaaaddd remove all 'a' = ddd

Since I cannot use replace or remove method I have no idea how to start this coding...

Solutions

Expert Solution

StringOperations.java

import java.util.ArrayList;
import java.util.Scanner;

public class StringOperations {
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String inp = sc.nextLine().trim();
  
// reverse a string
System.out.println(inp + " in reverse order is: " + reverse(inp));
  
// replace 1st and 2nd 'a' with 'b'
System.out.println("Replacing 1st a in " + inp + " with b gives: " + replace(inp, 'a', 'b', 1));
System.out.println("Replacing 2nd a in " + inp + " with b gives: " + replace(inp, 'a', 'b', 2));
  
// remove 1st and 2nd 'a'
System.out.println("Removing 1st a from " + inp + " gives: " + remove(inp, 'a', 1));
System.out.println("Removing 2nd a from " + inp + " gives: " + remove(inp, 'a', 2));
  
// remove all 'a' from a string
System.out.println("Removing all a's from " + inp + " gives: " + removeAll(inp, 'a'));
System.out.println("Removing all a's from aaaaddd gives: " + removeAll("aaaaddd", 'a'));
}
  
private static String reverse(String s)
{
String res = "";
for(int i = s.length() - 1; i >= 0; i--)
{
res += s.charAt(i);
}
return res;
}
  
private static String replace(String s, char target, char ch, int pos)
{
String res = "";
int count = 0;
int index = -1;
ArrayList<Character> chars = new ArrayList<>();
  
for(int i = 0; i < s.length(); i++)
{
chars.add(s.charAt(i));
if(s.charAt(i) == target)
{
count++;
  
if(count == pos)
index = i;
}
}
  
if(count == 0 || pos < 0 || pos > count || index == -1)
res = "";
else
chars.set(index, ch);
  
for(Character c : chars)
{
res += c;
}
  
return res;
}
  
private static String remove(String s, char target, int pos)
{
String res = "";
int count = 0;
int index = -1;
ArrayList<Character> chars = new ArrayList<>();
  
for(int i = 0; i < s.length(); i++)
{
chars.add(s.charAt(i));
if(s.charAt(i) == target)
{
count++;
  
if(count == pos)
index = i;
}
}
  
if(count == 0 || pos < 0 || pos > count || index == -1)
res = "";
else
chars.set(index, '\0');
  
for(Character c : chars)
{
res += c;
}
  
return res;
}
  
private static String removeAll(String s, char target)
{
String res = "";
  
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) != target)
res += s.charAt(i);
}
  
return res;
}
}

******************************************************************** SCREENSHOT *******************************************************


Related Solutions

Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
write a program that replace each line of a file with its reverse. for example, if...
write a program that replace each line of a file with its reverse. for example, if you run: java Reverse HelloPrinter.java then the contents of HelloPrinter.java are changed to retnirPolleH ssalc clibup { )sgra ][gnirtS(niam diov citats clibup { wodniw elosnoc eht ni gniteerg a yalpsiD // ;) "!dlroW ,olleH " (nltnirp.tuo.mestyS } }
I need to make a strategic IT plan for Emirates Airline so I need the organization...
I need to make a strategic IT plan for Emirates Airline so I need the organization background answering the following points: Date that the company was founded. Outline, in point form, a brief history of your company. Outline any acquisitions or changes in ownership. Are any changes of ownership anticipated? What are the major challenges the company has overcome to date? In the last 5 years? In the last year? How would you describe your company in one sentence? Do...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
B has to be matched with A so please I need both in Java. the previous...
B has to be matched with A so please I need both in Java. the previous project mean A A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT