The Programming Project: January 2026

Tuesday, January 6, 2026

ISC Computer Science Theory Paper 2023 Sorting of Words in a String


 




import java.io.*;

public class String2023 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        SortAlpha obj = new SortAlpha(br);
        obj.acceptsent();
        obj.sort();
        br.close();
    }
}

class SortAlpha {

    private String sent;
    private int n;
    private BufferedReader br;
    private String[] words;

    SortAlpha(BufferedReader br) {
        this.br = br;
        this.sent = "";
        this.n = 0;
    }

    public void sort() {
        words = new String[n];
        int k = 0;
        // extracting the words
        for (int i = 0; i < sent.length(); i++) {
            words[k] = "";
            while (sent.charAt(i) != ' ' && i < sent.length()) {
                words[k] += sent.charAt(i);
                i++;
                if (i == sent.length()) {
                    break;
                }

            }
            if (i < sent.length()) {
                while (sent.charAt(i) == ' ' && i < sent.length()) {
                    i++;
                }
            }
            i -= 1;
            k++;
            if (k == n) {
                break;
            }
        }
        // sorting the array of words
        String temp = "";
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (words[j].compareTo(words[j + 1]) > 0) {
                    temp = words[j];
                    words[j] = words[j + 1];
                    words[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.print(words[i] + " ");
        }
    }

    // Added 'throws IOException' here to fix the error
    public void acceptsent() throws IOException {
        System.out.println("Enter the sentence in uppercase:");
        sent = br.readLine();

        if (sent == null || sent.trim().isEmpty()) {
            System.out.println("Word count: 0");
            return;
        }

        // Counting words by checking for spaces
        for (int i = 0; i < sent.length() - 1; i++) {
            if (sent.charAt(i) == ' ' && sent.charAt(i + 1) != ' ') {
                n++;
            }
        }

        n += 1;
        System.out.println("Number of words: " + n);
    }
}


USING LIBRARY FUNCTIONS

import java.io.*;
import java.util.Arrays;

public class String2023 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        SortAlpha obj = new SortAlpha(br);
        obj.acceptsent();
        obj.sort();
        br.close();
    }
}

class SortAlpha {
    private String sent;
    private int n;
    private BufferedReader br;
    private String[] words;

    SortAlpha(BufferedReader br) {
        this.br = br;
        this.sent = "";
        this.n = 0;
    }

    public void sort() {
        if (n == 0) return;
       
        // Use split() to extract words
        words = sent.trim().split("\\s+");
       
        // Use Arrays.sort() for sorting
        Arrays.sort(words);
       
        // Print sorted words
        System.out.print("Sorted words: ");
        for (int i = 0; i < n; i++) {
            System.out.print(words[i] + " ");
        }
        System.out.println();
    }

    public void acceptsent() throws IOException {
        System.out.println("Enter the sentence in uppercase:");
        sent = br.readLine();

        if (sent == null || sent.trim().isEmpty()) {
            System.out.println("Word count: 0");
            return;
        }

        // Validate uppercase
        if (!sent.matches("[A-Z\\s]+")) {
            System.out.println("Error: Only uppercase letters and spaces allowed!");
            sent = "";
            n = 0;
            return;
        }

        // Use split() and length to count words
        words = sent.trim().split("\\s+");
        n = words.length;
       
        System.out.println("Number of words: " + n);
    }
}