Monday , 27 February 2017
Home » Class 10 » Program to Remove Consecutive Repeated Characters from a Word

Program to Remove Consecutive Repeated Characters from a Word

Question:

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.

Example:

INPUT – Jaaavvvvvvvvaaaaaaaaaaa
OUTPUT – Java

INPUT – Heeeiiiissggoiinggg
OUTPUT – Heisgoing

Programming Code:

/**
 * The class RemoveRepChar inputs a word and replaces the sequence of repeated characters
 * by its single occurrence
 * @author : www.javaforschool.com
 * @Program Type : BlueJ Program - Java
 */

import java.io.*;
class RemoveRepChar
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word: "); // Inputting the word
        String s = br.readLine();

        s = s + " "; // Adding a space at the end of the word
        int l=s.length(); // Finding the length of the word
        String ans=""; // Variable to store the final result
        char ch1,ch2;

        for(int i=0; i<l-1; i++)
        {
            ch1=s.charAt(i); // Extracting the first character
            ch2=s.charAt(i+1); // Extracting the next character

// Adding the first extracted character to the result if the current and the next characters are different

            if(ch1!=ch2)
            {
            ans = ans + ch1;
            }
        }
        System.out.println("Word after removing repeated characters = "+ans); // Printing the result
    }
}

Output:

Example 1:
Enter any word: Jaaavvvvvvvvaaaaaaaaaaa
Word after removing repeated characters = Java

Example 2:
Enter any word: iiiiiisssssfffffffffffffuunnnnn
Word after removing repeated characters = isfun

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

10 comments

  1. I think in the code you have written the last character will not get printed as in the example of jjjjjjjjjjaaaaaaaaaaaaaaaavvvvvvvvvvvvvvvvaaaaaaaaaaa the last time ch1!=ch2 will be when ch1 is v and ch2 is a in that case it will concatenate v to the resulting string but after that there will be no case when ch1!=ch2 hence a will not concatenate to the string.

  2. michael.mrperfect@gmail.com

    I did not undrstnd y u added the space to the accepted string. plz. answr fast

    • We added a space at the end of the inputted string because we are checking every character with the next character. So using this method, the last character will be left out as it will have no other character after it to check with. This is why we are adding a space to make up for this.

  3. Love the programs on this website. Very simple logic followed.

  4. Hey JfS, Thanks a lot I was searching for this program after many failed attempts. Can you please give me the link where I can find specimen paper for ICSE 2014. 🙂

  5. programe for removing duplicate elements in  a string is not working

    • What problem are you facing?
      Note: The program given in this website is for removing repeated characters by replacing the sequence of repeated characters by its single occurrence.
      So, jaaaaaaaaaaavavvvvvaaaaaaaa will give an output: “javava” and not “Jav”

Leave a Reply to Java For School Cancel reply

Your email address will not be published. Required fields are marked *