Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A character double occurs when the same character occurs twice in a row, such as

ID: 3755677 • Letter: A

Question

A character double occurs when the same character occurs twice in a row, such as the "mm" in "programming". Note the character could be a symbol, such as "--" or "!!". For example, the words below all contain character doubles: Hello, good, x++, follow… Write a full Java program that counts the number of character doubles in a phrase entered by the user. The output should look like the example below. Enter phrase: Follow Programming Concepts!! Output: There are 3-character doubles. You do not have to worry about detecting upper vs. lower-case letters, nor do you have to handle the case when a character occurs three times or more in a row

Explanation / Answer


import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/
public class CharDoubles {
  
  
public static void main(String argv[])
{
  
Scanner sc = new Scanner(System.in);///to read
  
String s;
//reading string
  
System.out.print("Enter phrase:");
  
s =sc.nextLine();//reading line
  
  
int count=0;//to store the count of chardoubles
  
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))//checking consecutive chars
//if equal then increasing count
count++;
  
}
  
  
//displaying output
System.out.println("Output:"+count+"-character doubles. ");
  
}
}

//PLS give a thumbs up if you find this helpful, its helps alot, thanks

output/l

run:
Enter phrase:Follow Programming Concepts!!
Output:3-character doubles.

BUILD SUCCESSFUL (total time: 3 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote