Remove Repeated Alphabets Java Program | ISC Computer Science 2021 Paper 1

A class Repeat has been defined to perform string related operations on a word.

Some of the members of the class are given below:

Class name: Repeat
Data members/instance variables:
str: to store a word
len: integer to store the length of the word
newstr: to store the modified word
Methods/Member functions:
Repeat(): default constructor to initialize data members with legal initial values
void readword(): to accept a word in uppercase
void modify(): displays and removes the repeated alphabets from the word stored in ‘str’ and stores the modified word in ‘newstr’ without the repeated alphabets.
Example: ASSESSMENT (repeated alphabets are ‘S’, ‘E’ and modified word becomes AMNT by removing the repeated alphabets).
void show(): displays the original word along with the modified word

Specify the class Repeat giving details of the constructor(), void readword(), void modify() and void show(). Define a main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class Repeat{
    String str;
    int len;
    String newstr;
    public Repeat(){
        str = "";
        newstr = "";
        len = 0;
    }
    public void readword(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        str = in.nextLine().toUpperCase();
        len = str.length();
    }
    public void modify(){
        for(int i = 0; i < len; i++){
            char ch = str.charAt(i);
            int count = 0;
            for(int j = 0; j < len; j++){
                if(ch == str.charAt(j))
                    count++;
            }
            if(count == 1)
                newstr += ch;
        }
    }
    public void show(){
        System.out.println("Original word: " + str);
        System.out.println("Modified word: " + newstr);
    }
    public static void main(String args[]){
        Repeat obj = new Repeat();
        obj.readword();
        obj.modify();
        obj.show();
    }
}

Leave a Reply

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