Computer Science Department(Laboratory Workbook for Computer) Exercise-1Solution | AskTheCode

Java program that perform various operation on Strings | Problem Solving in Java | AskTheCode

Problem:

You will create a class named MyString that will perform different processing on Strings that are sent to it. All of the methods you create will be static, and the class should work in a similar manner to the Math class. Only the four methods listed below should be public.

1) Create a method reverseString that receives a String and returns a String that is the exact reversal of the characters in the first String.


2) Create a method isPalindrome that receives a String and returns a boolean value of true if the String is a Palindrome and false if it is not. A word is a palindrome if it reads the same forwards and backwards. For example, the word level is a palindrome. The idea of a palindrome can be extended to phrases or sentences if we ignore details like punctuation, so you need to create a method that cleans the sentence from punctuation and spaces, then convert the sentence to lower or upper case after that we can use the method is Palindrome with the new sentence. Here are two familiar examples:

Madam, I'm Adam → madamimadam

A man, a plan, a canal: Panama → amanaplanacanalpanama


3) Create a method ShortHanded that receives a String and returns the String converted into shorthand. The simplified shorthand form of a string is defined as follows:

  1. Replace these four words: "and" with "&", "to" with "2", "you" with "U", and "for" with "4".

  2. Remove all vowels ('a', 'e', 'i, 'o', 'u', whether lowercase or uppercase), do not remove 'u' and 'I' if they did not occurred in a word.

4) Also add the following methods:

  1. numberOfSentences: a method that accepts a String and return number of sentences on it. A sentence can be determined if it ends with one of those punctuation ( . , ? ! ).

  2. numberOfWords: a method that accepts a String and return number of words. A word consists of more than 3 characters.

Sample input:

Hello there, here is your solution. Do check. If it helped, show your support by liking this post. Until then, Happy Coding

Sample output:

The string input is:

Hello there, here is your solution. Do check. If it helped, show your support by liking this post. Until then, Happy Coding

Reversed output of the string is:

gnidoC yppaH ,neht litnU .tsop siht gnikil yb troppus ruoy wohs ,depleh ti fI .kcehc oD .noitulos ruoy si ereh ,ereht olleH

String input is palindrom? false

ShortHanded output of the string is:

Hll thr, hr s yr sltn. D chck. If t hlpd, shw yr spprt by lkng ths pst. Until thn, Hppy Cdng

Number of sentences is 7

Number of words is 17

Code:

import java.io.*;
import java.util.*;

class MyString{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string:");
		String str = br.readLine();

		System.out.println("\nThe string input is:\n"+str);
		System.out.println("Reversed output of the string is:\n"+reverseString(str));
		System.out.println("String input is palindrom? "+isPalindrome(str));
		System.out.println("ShortHanded output of the string is:\n"+ShortHanded(str));
		countSenten_and_Words(str);
	}

	public static String reverseString(String s){
		String revString = "";
		for (int i = s.length()-1; i >= 0; i--) {
			revString += s.charAt(i);
		}
		return revString;
	}

	private static String trimPunct(String str){
		String res = "";
		for (char ch : str.toCharArray()) {
			if (ch == ' ' || ch == ',' || ch == '.' || ch == '?' || ch == '!' || ch == '\'' || ch == ';' 
				|| ch == '-' || ch == ':') {
				continue;
			}
			res += ch;
		}
		res = res.toLowerCase();
		return res;
	}
	public static boolean isPalindrome(String s){
		String revStr = reverseString(trimPunct(s));
		if (revStr.equals(trimPunct(s)))
			return true;
		return false;
	}

	private static String removeVowels(String str){
		String res = "";
		for (String s : str.split(" ")) {
			if (s.charAt(0) == 'U' || s.charAt(0) == 'I') {
				res += s + " ";
				continue;
			}
			res += s.replaceAll("[aeiouAEIOU]", "") + " ";
		}
		return res;
	}
	public static String ShortHanded(String s){
		TreeMap<String, String> dict = new TreeMap<>();
		dict.put("and", "&");
		dict.put("to", "2");
		dict.put("you", "U");
		dict.put("for", "4");
		String res = "";
		for (String str : s.split(" ")) {
			if (dict.containsKey(str)) {
				res = res + dict.get(str).toString() + " ";
			}
			else{
				res = res + str + " ";
			}
		}
		return removeVowels(res);
	}

	private static int numberOfSentence(String str){
		int count = 0;
		for (String s : str.split("[.,?!]")) {
			count++;
		}
		return count;
	}
	private static int numberOfWords(String str){
		int count = 0;
		for (String s : str.split(" ")) {
			if (s.length() > 3)
				count++;
		}
		return count;
	}

	public static void countSenten_and_Words(String str){
		System.out.println("Number of sentences is "+numberOfSentence(str)+
			"\nNumber of words is "+numberOfWords(str));
	}
}

Note: This code counts the occurrence based on the instructions provided in the problem statement.

33 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes