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

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 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 } }
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...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT