Write program to remove consecutive duplicate characters in a string?

Posted On: Jan 07, 2021


A Program to remove consecutive duplicate characters in a string is as follows:

// Recursive Program to remove consecutive 
// duplicates from string S. 
#include <bits/stdc++.h> 
using namespace std; 
// A recursive function that removes  
// consecutive duplicates from string S 
void removeDuplicates(char* S) 
    // When string is empty, return 
    if (S[0] == '\0') 
    // if the adjacent characters are same 
    if (S[0] == S[1]) { 
        // Shift character by one to left 
        int i = 0;  
        while (S[i] != '\0') { 
            S[i] = S[i + 1]; 
        // Check on Updated String S 
    // If the adjacent characters are not same 
    // Check from S+1 string address 
    removeDuplicates(S + 1); 
// Driver Program 
int main() 
    char S1[] = "geeksforgeeks"; 
    cout << S1 << endl; 
    char S2[] = "aabcca"; 
    cout << S2 << endl; 
    return 0; 




    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    HCL Java Developer Interview Questions

    What is Multithreading in Java?

    Multithreading is a programmable approach to achieve multitasking. Multithreading in Java is a process of executing multiple threads cumulatively. A thread is the smallest unit of processing which is ...

    HCL Java Developer Interview Questions

    What use of intern() method in String?

    In Java, intern() is a native method of the String class. The String.intern () returns a reference to the equal string literal present in the string pool. The intern () method is applicable to String ...

    HCL Java Developer Interview Questions

    What is Collection Framework?

    A Java collection framework is an architecture that was added to Java 1.2 version. The Collection Framework provides a well-designed set of classes and interfaces for storing and manipulating a group ...