Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Replacing characters in Java without replace function. So I am asking for a string to be inputted, then I am asking what the user wants

Replacing characters in Java without replace function.

So I am asking for a string to be inputted, then I am asking what the user wants to do to the string, in this case, I am trying to replace the first occurrence of a character. I can ask for what wants to be replaced and what to replace it with. I also know how to do the replacement somewhat, but say if the string has 2 of the same characters, I get a new String for both. I cannot use the replace function, index function. Only length, concat, substring, length, and equals

I only need the first one.

*Eclipse Java Photon*

image text in transcribed

Output

image text in transcribed

else if (UserCommand.equalsIgnoreCase("replace first")) { System.out.println("Enter the character to replace"); Replace = input.next(); System.out.println("Enter the new character"); Replacement = input.next(); for (int i = 0; i < String.length();i++) { if (String.charAt(i)==Replace.charAt(0)) { StringEdit } = String.substring(0,i) + Replacement + String.substring(i + Replace.length()); System.out.print(StringEdit); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions