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

I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
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.
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...
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 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 need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT