Write a program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence. code example
Example: Write a program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence.
//java program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence
import java.util.*;
class Remove_Rep_Char
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any word: ");
String s = sc.nextLine();
s = s + " ";
int l=s.length();
String ans="";
char ch1,ch2;
for(int i=0; i<l-1; i++)
{
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2)
{
ans = ans + ch1;
}
}
System.out.println("Word after removing repeated characters = "+ans);
}
}