AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-string-handling: Java Program to print the following characteristics of the String

Problem Statement :
Write a program that will print the following charactaristics of the String
1. length of the string
2. print only the first half of the string2. (If length is an odd number,(length-1)/2 )
3. Remove all spaces(if any) in the line and print
4. Replace all the vowels with "_"

Input Specification :
Input will be a line of text with only
alphabetic characters in it

Output Specification :
If input text has any non-alphabetic character, Print "Invalid Input". Else,
First line should print the length of the string
Second line is the first half of the string
Third line is the input text without spaces
Fourth line is the input text with all the vowels replaced by "_"

Sample Input
This is a sample input line of text

Sample Output
35
This is a sample
Thisisasampleinputlineoftext
Th_s _s _ s_mpl_ _np_t l_n_ _f t_xt

corejava x 348
string-handling x 34
Posted On : 2017-07-22 17:25:39.0
profile Divesh - anyforum.in Divesh
102160
up-rate
9
down-rate

Answers


import java.util.Scanner;

public class StringCharacteristics {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String inputLine=input.nextLine();
if(inputLine.matches("^(?!\\s)[-a-zA-Z ]*(?<!\\s)$")){
System.out.println(inputLine.length());
System.out.println(inputLine.substring(0,inputLine.length()%2==0?inputLine.length()/2:(inputLine.length()-1)/2));
System.out.println(inputLine.replaceAll(" ", ""));
System.out.println(inputLine.replaceAll("[a,e,i,o,u,A,E,I,O,U]", "_"));
}else{
System.out.println("Invalid Input!");
}
}
}

Posted On : 2017-07-23 14:32:41
Satisfied : 10 Yes  23 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523186220593
Reply This Thread
up-rate
2
down-rate
Comments
There is some error on line 3
profile P.Ashutosh Mallick - anyforum.in P.Ashutosh Mallick
0  0  0
Posted On :2017-10-18 22:56:16.0
Leave a Comment
Ashutosh,
there is no error on line no 3. I think you missed import statement for Scanner class.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1862  20593
Posted On :2017-10-18 22:57:46.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in