Pendulum String Program | ISC Computer Science 2026 Theory

Design a class PendulumS to perform an operation on a word containing alphabets in uppercase only. Rearrange the word by putting the lowest ASCII value character at the centre and the second lowest ASCII value character to its right and the third to its left and so on.

Example 1:
INPUT: COMPUTER
OUTPUT: TPMCEORU

Example 2:
INPUT: SCIENCE
OUTPUT: SIECCEN

The details of the members of the class are given below:
Class name: PendulumS
Data members/instance variables:
wrd: to store the original word
newwrd: to store the rearranged word
Methods/Member functions:
PendulumS(String k): parameterised constructor to initialise wrd = k and newwrd = “”
int minCharIndex(String str): to find the index of the minimum ASCII value character in str and return it
void arrange(): to rearrange the characters of wrd as per the given instructions and store it in newwrd by invoking minCharIndex()
void display(): to display the original word and the rearranged word

Specify the class PendulumS giving the details of the constructor, int minCharIndex(String), void arrange() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class PendulumS{
    String wrd;
    String newwrd;
    public PendulumS(String k){
        wrd = k;
        newwrd = "";
    }
    public int minCharIndex(String str){
        int i = 0;
        int index = 0;
        int ascii = str.charAt(0);
        while(i < str.length()){
            char ch = str.charAt(i);
            if(ascii > ch){
                ascii = ch;
                index = i;
            }
            i++;
        }
        return index;
    }
    public void arrange(){
        int len = wrd.length();
        for(int i = 0; i < len; i++){
            int index = minCharIndex(wrd);
            char ch = wrd.charAt(index);
            if(i == 0 || i % 2 == 1)
                newwrd += ch;
            else
                newwrd = ch + newwrd;
            wrd = wrd.substring(0, index) + wrd.substring(index + 1);
        }
    }
    public void display(){
        System.out.println("Original word: " + wrd);
        arrange();
        System.out.println("Rearranged word: " + newwrd);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word in uppercase: ");
        String w = in.next().toUpperCase();
        PendulumS obj = new PendulumS(w);
        obj.display();
    }
}