Remove Consecutive Repeated Characters Program in Java and Python
Remove consecutive repeated characters program with algorithm, explanation, Java solution and simple Python solution.
Question:
Write a program to input a word from the user and remove the consecutive repeated characters by replacing each sequence of repeated characters by its single occurrence.
Algorithm:
Step 1: Start.
Step 2: Accept a word from the user.
Step 3: Add one blank space at the end of the word so that the last real character can be compared safely.
Step 4: Store the length of the modified word.
Step 5: Initialize an empty string answer.
Step 6: Run a loop from index 0 to length - 2.
Step 7: In each pass, extract the current character and the next character.
Step 8: Compare the two characters.
Step 9: If the current character is different from the next character, append the current character to answer.
Step 10: If both characters are the same, do not append the current character because it is part of a repeated sequence.
Step 11: Continue until all adjacent character pairs have been checked.
Step 12: Display answer as the word after removing consecutive repeated characters.
Step 13: Stop.
Explanation:
This program removes only consecutive repeated characters. It is different from removing all duplicate characters in a word. For example, in Mississippi, all repeated characters may be removed to get Misp, but that is not the aim of this program. Here the program compresses a continuous run of the same character into one character. So Jaaavvvvvvvvaaaaaaaaaaa becomes Java because the repeated a, v and final a groups are each replaced by a single occurrence.
The logic is based on comparing each character with the character immediately after it. If the current character and the next character are the same, the current character is not added to the result. This is because another copy of the same character follows it, so the sequence has not ended yet. When the current character becomes different from the next one, it means the current repeated group has ended, and one copy of that character should be stored in the answer.
For the word Jaaavvvvvvvvaaaaaaaaaaa, the first character J is compared with a. Since they are different, J is added. The first two a characters are skipped because each one is followed by another a. The last a before v is added because the repeated group of a ends there. The same idea is used for the group of v characters and the final group of a characters.
A blank space is added at the end of the word to make the last real character easy to process. Without this extra character, the loop would not be able to compare the last character with a next character. The blank space works as a safe final separator, so the last character of the actual word is added when it differs from the added space. This method uses only simple string indexing and adjacent comparison, making it suitable for ICSE and ISC string-processing practice.
Java Program:
/**
* The class RemoveRepChar inputs a word and replaces every sequence of
* consecutive repeated characters by a single occurrence.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class RemoveRepChar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any word: ");
String word = sc.nextLine();
/*
* A blank space is added at the end so that the last real character
* can also be compared with a next character.
*/
word = word + " ";
int length = word.length();
String answer = "";
for(int i = 0; i < length - 1; i++)
{
char current = word.charAt(i);
char next = word.charAt(i + 1);
/*
* Add the current character only when the repeated group ends.
* If current and next are the same, the group is still continuing.
*/
if(current != next)
{
answer = answer + current;
}
}
System.out.println("Word after removing repeated characters = " + answer);
}
}Equivalent Python Program:
word = input("Enter any word: ")
# Add a blank space so that the last character can also be compared.
word = word + " "
answer = ""
length = len(word)
for i in range(0, length - 1):
current = word[i]
next_char = word[i + 1]
# Store the character only when its repeated group ends.
if current != next_char:
answer = answer + current
print("Word after removing repeated characters =", answer)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.