Computer Science
While typing, a typist has created two or more consecutive blank spaces between the words of a sentence. Write a program in Java to eliminate multiple blanks between the words by a single blank.
Sample Input:
Indian Cricket team tour to Australia
Sample Output:
Indian Cricket team tour to Australia
Answer
import java.util.*;
public class KboatMultipleBlanks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();
int len = str.length();
StringTokenizer st = new StringTokenizer(str);
String newStr = ""; //Empty String
while (st.hasMoreTokens()) {
String word = st.nextToken();
newStr += word + " ";
}
newStr = newStr.trim();
System.out.println("Output String:");
System.out.println(newStr);
}
}
Output
Related Questions
The input in this question will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (') apostrophe, (.) full stop (, ) comma, (; ) semicolon, (:) colon and white space characters (blank, new line). Your task is to print the words of the text in reverse order without any punctuation marks other than blanks.
For example, consider the following input text:
This is a sample piece of text to illustrate this question.
If you are smart you will solve this right.The corresponding output would read as:
right this solve will you smart are you if question this illustrate to text of piece sample a is this
Note: Individual words are not reversed.
Input Format:
This first line of input contains a single integer n ( < = 20), indicating the number of lines in the input.
This is followed by lines of input text. Each line should accept a maximum of 80 characters.Output Format:
Output the text containing the input lines in reverse order without punctuations except blanks as illustrated above.
Test your program for the following data and some random data.
Sample Data:
Input:
2
Emotions controlled and directed to work, is character.
By Swami Vivekananda.Output:
Vivekananda Swami By character is work to directed and controlled EmotionsInput:
1
Do not judge a book by its cover.Output:
cover its by book a judge not DoWrite a program in Java to accept a four-letter word. Display all the probable four letter combinations such that no letter should be repeated in the output within each combination.
Sample Input:
PARKSample Output:
PAKR, PKAR, PRAK, APRK, ARPK, AKPR, and so on.Write a program in Java to accept two strings. Display the new string by taking each character of the first string from left to right and of the second string from right to left. The letters should be taken alternatively from each string. Assume that the length of both the strings are same.
Sample Input:
String 1: HISTORY
String 2: SCIENCESample Output:
HEICSNTEOIRCYSA new advanced Operating System, incorporating the latest hi-tech features has been designed by Opera Computer Systems. The task of generating copy protection code to prevent software privacy has been entrusted to the Security Department. The Security Department has decided to have codes containing a jumbled combination of alternate uppercase letters of the alphabet starting from A up to K (namely among A, C, E, G, I, K). The code may or may not be in the consecutive series of alphabets. Each code should not exceed 6 characters and there should be no repetition of characters. If it exceeds 6 characters, display an appropriate error message.
Write a program to input a code and its length. At the first instance of an error display "Invalid" stating the appropriate reason. In case of no error, display the message "Valid".
Sample Data:
Input:
n=4
ABCEOutput:
Invalid! Only alternate letters permitted!Input:
n=4
AcIKOutput:
Invalid! Only uppercase letters permitted!Input:
n = 7Output:
Error! Length of String should not exceed 6 characters!Input:
n=3
ACEOutput:
ValidInput:
n=5
GEAIKOutput:
Valid