|
| 1 | +import java.util.Scanner; |
| 2 | +class SplitWithLength { |
| 3 | + // Method to calculate the length of the String |
| 4 | + public static int stringLength(String s) { |
| 5 | + int count = 0; |
| 6 | + try { |
| 7 | + while (true) { |
| 8 | + s.charAt(count); // Try to access the character at index 'count' |
| 9 | + count++; // Increment the counter if possible. |
| 10 | + } |
| 11 | + } catch (IndexOutOfBoundsException e) { |
| 12 | + // Exception occurs when we go beyond the last index, so return count |
| 13 | + } |
| 14 | + return count; |
| 15 | + } |
| 16 | + |
| 17 | + // Method to split the string into words using charAt method |
| 18 | + public static String[] splitString(String s, int length) { |
| 19 | + // calculate the number of words in the String |
| 20 | + int wordCount = 0; |
| 21 | + for(int i=0;i<length;i++) { |
| 22 | + if(s.charAt(i) == ' ') { |
| 23 | + wordCount++; |
| 24 | + } |
| 25 | + } |
| 26 | + wordCount++; // For the last word |
| 27 | + |
| 28 | + // Create an array to store the indices of spaces |
| 29 | + int[] spaces = new int[wordCount]; |
| 30 | + int j = 0; |
| 31 | + for(int i=0;i<length;i++) { |
| 32 | + if(s.charAt(i) == ' ') { |
| 33 | + spaces[j++] = i; |
| 34 | + } |
| 35 | + } |
| 36 | + spaces[j] = length; |
| 37 | + |
| 38 | + // Create an array to store the words |
| 39 | + String[] words = new String[wordCount]; |
| 40 | + int start = 0; |
| 41 | + for(int i=0;i<wordCount;i++) { |
| 42 | + String word = ""; |
| 43 | + for(int k=start;k<spaces[i];k++) { |
| 44 | + word += s.charAt(k); |
| 45 | + } |
| 46 | + words[i] = word; |
| 47 | + start = spaces[i]+1; |
| 48 | + } |
| 49 | + return words; |
| 50 | + } |
| 51 | + |
| 52 | + // Method to create a 2D array with words and their corresponding lengths |
| 53 | + public static String[][] create2DArray(String[] words) { |
| 54 | + String[][] wordLengths = new String[words.length][2]; |
| 55 | + for(int i=0;i<words.length;i++) { |
| 56 | + wordLengths[i][0] = words[i]; |
| 57 | + wordLengths[i][1] = String.valueOf(stringLength(words[i])); |
| 58 | + } |
| 59 | + return wordLengths; |
| 60 | + } |
| 61 | + |
| 62 | + // Method to display the words and their lengths in a tabular format |
| 63 | + public static void displayTable(String[][] wordLengths) { |
| 64 | + System.out.println("\nWord\t\tLength"); |
| 65 | + System.out.println("-------------------"); |
| 66 | + for (String[] row : wordLengths) { |
| 67 | + System.out.println(row[0] + "\t\t" + Integer.parseInt(row[1])); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Main method |
| 72 | + public static void main(String[] args) { |
| 73 | + Scanner input = new Scanner(System.in); |
| 74 | + |
| 75 | + // Define a variable of type String and take user input |
| 76 | + System.out.print("Enter a string: "); |
| 77 | + String userInput = input.nextLine(); |
| 78 | + |
| 79 | + // Calculate the length of the string |
| 80 | + int length = stringLength(userInput); |
| 81 | + |
| 82 | + // Split the string into words |
| 83 | + String[] words = splitString(userInput, length); |
| 84 | + |
| 85 | + // Create a 2D array with words and their corresponding lengths |
| 86 | + String[][] wordLengths = create2DArray(words); |
| 87 | + |
| 88 | + // Display the words and their lengths in a tabular format |
| 89 | + displayTable(wordLengths); |
| 90 | + } |
| 91 | +} |
0 commit comments