Ask Question
26 September, 08:05

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)

+3
Answers (1)
  1. 26 September, 08:12
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers