SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Java Primer
Exercise:
Exercises
Question:9 | ISBN:9781118771334 | Edition: 6

Question

Write a short Java method that uses a StringBuilder instance to remove all the punctuation from a string s storing a sentence, for example, transforming the string "Let’s try, Mike!" to "Lets try Mike".

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package java_problems_datastructures;

import java.util.Scanner;

public class CountVowels {

	static String str;

	private static void enterString() {

		// we will just take input from the scanner don't do any operations on it.

		Scanner input = new Scanner(System.in);
		System.out.println("Enter a sring value: ");
		str = input.nextLine();

		input.close();

	}

	private static String removePunctuations(String string) {

		/*
		 * replace all method takes two arguments first one is regex pattern in which
		 * you mention pattern applicable to remove all punctions in the string and in
		 * the second argument we pass the which one we want to replace with regex
		 * pattern to remove all punctuations look like below.
		 */
		string = string.replaceAll("\\p{P}", "");

		return string;

	}

	public static void main(String[] args) {

		enterString();
		// print the modified string
		System.out.println("After removing the all punctuations in the string is: " + removePunctuations(str));

	}

}

 

0 0

Discussions

Post the discussion to improve the above solution.