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
1
Expert's answer
2016-01-19T08:28:40-0500
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();
Comments
Leave a comment