C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Double Letter Sequences in a String Program - Java Programs

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

import java.util.Scanner; class Sequence{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.print("Enter the string: "); String s = in.nextLine().toUpperCase(); int count = 0; for(int i = 0; i < s.length() - 1; i++){ char a = s.charAt(i); char b = s.charAt(i + 1); if(a == b) count++; } System.out.println(count); } }

Output

 
 OUTPUT : 
Enter the string: SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
4