62 572
Assignments Done
99%
Successfully Done
In June 2018

Answer to Question #56490 in Java | JSP | JSF for Khushi Singh

Question #56490
Write a program in Java to accept a string and display only those characters which are consecutives.
Sample Input : UNDERSTANDING COMPUTER APPLICATIONS
Sample Output : D,E
R,S
S,T are consecutive characters
Expert's answer
import java.io.*;

public class StringChecker {

// a method that checks characters and prints consecutive characters
public static void printConsecutiveCharacters(char[] chars){
for (int i = 1; i < chars.length; i++) {
if(chars[i]-1==(chars[i-1])){
System.out.println(chars[i-1] + " " + chars[i]);
}
}
}

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a string");
String str =reader.readLine();
String [] words = str.split("[ ; /? , : \" /' . \n]"); //splitting a string to ignore punctuation
String concatWords ="";
//creating a new string without punctuations and white spaces
for (int i = 0; i <words.length ; i++) {
concatWords+=words[i];
}

char [] chars;
concatWords = concatWords.toUpperCase();//present all characters in upper case to ignore the case of the characters
chars = concatWords.toCharArray();

System.out.println("Consecutive characters: ");

printConsecutiveCharacters(chars);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions