Write a program to input a sentence. Convert the sentence into upper case letters. Display the words along with frequency of the words which have at least a pair of consecutive letters. Sample Input: MODEM IS AN ELECTRONIC DEVICE Sample Output: MODEM DEVICE Number of words containing consecutive letters: 2 in java
Answers
Explanation:
Program to input a sentence. Convert the sentence into upper case letters. Display the words along with frequency of the words which have at least a pair of consecutive letters. Sample Input: MODEM IS AN ELECTRONIC DEVICE Sample Output: MODEM DEVICE Number of words containing consecutive.
Answer:
import java.util.*;
class consecutiveLetters
{
public static void main()
{
System.out.println("Input a sentence");
Scanner S=new Scanner(System.in);
String Str=S.nextLine();
int len=Str.length();
int a=0;
int b=0;
String sub ="";
int frequency=0;
Str=Str.toUpperCase();
Str=Str+" ";
System.out.println(Str+"1");
for(int i=0;i<len;i++)
{
if(Str.charAt(i)==' ')
{
b=i;
sub=Str.substring(a,b);
a=b;
for(int j=0;j<sub.length()-1;j++)
{
int x=sub.charAt(j);
int y=sub.charAt(j+1);
if(x==(y-1))
{
System.out.println(sub);
frequency++;
}
}
}
}
System.out.println("Number of words containing consecutive letters: "+frequency);
}
}
Explanation: