Removing Repeated Letters From A String

Write a program to input a string from the user in lowercase form.

Then remove all the repeated letters from the string and keep only single occurrences of the characters present in the string.

Display the final string formed after the removal of the extra repeated characters.

Example:
INPUT: applications
OUTPUT: aplictons

import java.io.*;
class Repeated{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("String: ");
        String s = br.readLine().toLowerCase();
        String t = "";
        while(s.length() > 0){
            t += s.charAt(0);
            s = s.replace(Character.toString(s.charAt(0)), "");
        }
        System.out.println(t);
    }
}

2 thoughts on “Removing Repeated Letters From A String”

    • We first take one character from the string, place it to the new string, and then replace that character with an empty string.
      This is repeated as long as the original string has characters left in it.

Leave a Reply

%d bloggers like this: