Ask Question
13 May, 06:22

Write a program that removes all spaces from the given input. Ex: If the input is: Hello my name is John. the output is: HellomynameisJohn. Your program must define and call the following method. The method should return a string representing the input string without spaces. public static String removeSpaces (String userString)

+5
Answers (1)
  1. 13 May, 06:37
    0
    public class num2 {

    public static String removeSpaces (String word) {

    String wordNoSpaces = word. replaceAll (" ","");

    return wordNoSpaces;

    }

    public static void main (String[] args) {

    String str = "Hello my name is John";

    System. out. println (removeSpaces (str));

    }

    }

    Explanation:

    Using Java Prograamming language

    Define the method removeSpaces () to accept a string as parameter and return a string as well With the method's body, use Java's built-in method replaceAll () to replace all whitespaces with no spaces String wordNoSpaces = word. replaceAll (" ",""); Return the resulting string In the main method define a string with white spaces Call removeSpaces and pass the defined string as an argument Output the result
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that removes all spaces from the given input. Ex: If the input is: Hello my name is John. the output is: HellomynameisJohn. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers