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...
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
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 to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
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 } }
How can I make sure (test) a reverse program that returns the same array with the...
How can I make sure (test) a reverse program that returns the same array with the same values as you entered before, but just in reverse order? Example of the program im talking about: void reverseArray(int arr[], int start, int end) {     int temp;     while (start < end)     {         temp = arr[start];            arr[start] = arr[end];         arr[end] = temp;         start++;         end--;     }    }  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT