how to stop repetation of carcters in cpp code example
Example 1: Write a program to input a word from the user and remove the duplicate characters present in it.
import java.util.*;
class RemoveDuplicate
{
public static void main(String args[])
{
String s,ans="";
char ch ;
int l,i=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any string: ");
s = sc.nextLine();
l=s.length();
s+=" ";
ch=s.charAt(0);
while(i<l)
{
ans= ans+ ch;
while(s.charAt(++i)==ch && i<l)
{}
ch=s.charAt(i);
}
System.out.println("String after removing repeated characters : \n"+ans);
}
}
Example 2: removing repeated characters in a string c++
s.erase(std::unique(s.begin(), s.end()), s.end());