1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pishuonlain [190]
3 years ago
12

How would I do this code with a while loop to remove the letters? Please and thank you

Computers and Technology
1 answer:
dusya [7]3 years ago
7 0

There is of course a replace() function in java that lets you do this without a while loop. If you want to do it letter by letter, it gets a little complicated because you have to take care not to index the string outside its bounds.

I did not bother to create a separate runner class. The Main is just inside this class and everything is static.

public class LetterRemover {

   public static String RemoveLetters(String s, char c)

   {

       int loc = s.indexOf(c);

       while(loc >= 0)

       {            

           if (loc == 0) {

               s = s.substring(1);

           } else if (loc == s.length()-1) {

               s = s.substring(0, loc);

           } else {

               s = s.substring(0, loc) + s.substring(loc+1);

           }

           loc = s.indexOf(c);

       }

       return s;

   }

   public static void Test(String s, char c) {

       System.out.format("%s - letter to remove %c\n%s\n", s, c, RemoveLetters(s,c));

   }

   

   public static void main(String[] args) {

       Test("I am Sam I am", 'a');

       Test("ssssssssxssssesssssesss", 's');

       Test("qwertyqwertyqwerty", 'a');

   }

}

You might be interested in
Please describe how you can use the login page to get the server run two SQL statements. Try the attack to delete a record from
vekshin1
The only sure way to prevent SQL Injection attacks is input validation and parametrized queries including prepared statements. The application code should never use the input directly. ... Database errors can be used with SQL Injection to gain information about your database.
6 0
3 years ago
Write a program that accepts a whole number as
SashulF [63]

This is for Python

number = int(input('Number: '))

number = number * 12

print(number)

3 0
3 years ago
What is the Multiple Source Test
Andrews [41]
It's a test Where you can research your answers Like the internet,Library,etc to find your answers.
5 0
3 years ago
Read 2 more answers
20<br>Calculate the kinette energy of a body the speed<br>that will be a mass tragm​
jolli1 [7]

Answer:

20

Explanation:

4 0
3 years ago
Which of the following is one of the first two levels of formatting in a Word document?
Dafna1 [17]

Explanation:

Paragraph Formatting.

5 0
3 years ago
Other questions:
  • Multiple Choice
    6·1 answer
  • Hiiiiiiiiiii hiiiiiiiiiiiiiiiiiii
    9·2 answers
  • Richard needs to copy information from another slide presentation that uses a different design template. To ensure that the info
    7·1 answer
  • Encryption Using Rotate Operations Write a procedure that performs simple encryption by rotating each plaintext byte a varying n
    10·1 answer
  • 1) List at least five smaller behaviors you could break the complex behavior "brushing my teeth" into.
    14·2 answers
  • _ models are non visual ways of communicating how someone thinks about something in the natural world
    12·1 answer
  • cout &lt;&lt; "Part 1" &lt;&lt; endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a spac
    11·1 answer
  • Please help me. Adnan also wants to add a photograph of a bridge. It is on another PowerPoint presentation that is open in a dif
    11·1 answer
  • # 12/9/2020
    13·1 answer
  • Write a Python program (rainfall.py) to collect data and calculate the average rainfall over a period of years. The program shou
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!