Question
Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"
Sample Output: 4
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
String sen = sc.nextLine();
sen = sen.toUpperCase();
int count = 0;
for (int i = 1; i < sen.length(); i++)
{
if (sen.charAt(i) == sen.charAt(i - 1))
{
count++;
}
}
System.out.println(count);
}
}