diff options
author | Sudipto Mallick <> | 2024-01-02 02:12:16 +0000 |
---|---|---|
committer | Sudipto Mallick <> | 2024-01-02 02:12:16 +0000 |
commit | a70e0a59817ce06a3dd23b3750ae16ee6660deaf (patch) | |
tree | 0843bf4d30edf22c9c4c27ff4887351c85993ccd /java/code/StringOperations.java | |
parent | 63c589e826829c5f47f95a5642531e02b2b2c8f7 (diff) | |
download | zadania-a70e0a59817ce06a3dd23b3750ae16ee6660deaf.tar.gz |
Add Java assignments to the repository
.gitignore: The build files, files left by editors like *~ and the PDF documents for the assignments are to be ignored for the purposes of version control. README.rst: Rewrite, with one line description in Chinese. java/alist.txt: List of assignments, sorted in the order of the list of assignments, with the ones with completed documentation marked with `#`. java/buildall: Script that will build all of the assignments into one PDF file; to be rehauled for the final document. code/*.java: The Java source code for the assignments. dbld: Script that builds a PDF document for a single assignment. index.typ: The list of assignments, subject to update for further refinement and inclusion of yet more assignments. jbld: Script that compiles code for a single Java assignment. output/*.typ: Typst file containing the output (sessions) obtained from running the individual Java assignments. state.sql: Future alternative to `alist.txt`, under development. template.typ: The Typst template used across all of assignment, containing common code for the uniform styling, such as page border. text/*.typ: Typst file documenting each Java assignment. vendor/Java.sublime-syntax: Updated Sublime Text syntax file for Java, used for newer syntax features, such as `var`, not yet available in syntaxes shipped with Typst. vendor/gr.tmTheme: A grayscale TextMate theme to be used for code in the documents generated by Typst suitable for black and white printing. wltd: Difference between the files in `code/` and `text/`, and `code/` and `output`; need to be rewritten along with `state.sql`.
Diffstat (limited to 'java/code/StringOperations.java')
-rw-r--r-- | java/code/StringOperations.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/java/code/StringOperations.java b/java/code/StringOperations.java new file mode 100644 index 0000000..de817ce --- /dev/null +++ b/java/code/StringOperations.java @@ -0,0 +1,112 @@ +import java.util.Scanner; + +class MyString { + String str; + + MyString(String s) { str = s; } + + String valueOf() { return str; } + + int countWords() { + int count = 0, size = str.length(); + enum state { WORD, SPACE }; + state st = state.WORD; + for (int i = 0; i < size; i++) { + char c = str.charAt(i); + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { + st = state.SPACE; + } else if (st != state.WORD) { + st = state.WORD; + count++; + } + } + return count; + } + + MyString reverse() { + char[] arr = str.toCharArray(); + int end = arr.length - 1; + for (int i = 0; i < arr.length / 2; i++, end--) { + char tmp = arr[i]; + arr[i] = arr[end]; + arr[end] = tmp; + } + return new MyString(String.copyValueOf(arr)); + } + + MyString toLowerCase() { + char[] arr = str.toCharArray(); + char[] narr = new char[arr.length]; + for (int i = 0; i < arr.length; i++) { + char c = arr[i]; + if ('A' <= c && c <= 'Z') c += 'a' - 'A'; + narr[i] = c; + } + return new MyString(String.copyValueOf(narr)); + } + + boolean equals(MyString ms) { + char[] arr1 = str.toCharArray(); + char[] arr2 = ms.valueOf().toCharArray(); + if (arr1.length != arr2.length) return false; + for (int i = 0; i < arr1.length; i++) { + if (arr1[i] != arr2[i]) return false; + } + return true; + } + + boolean isCaseInsensitivePalindrome() { + return str.toLowerCase().equals(reverse().toLowerCase().valueOf()); + } +} + +class StringOperations { + static void menu() { + System.out.println( + "Options:\n" + + " 1. Re-enter a string\n" + + " 2. Display the string\n" + + " 3. Count words in the string\n" + + " 4. Reverse the string\n" + + " 5. Case-insensitively check whether the string is palindrome or not\n" + + " 6. Exit\n"); + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Menu-driven program for string operations"); + System.out.print("Enter a string: "); + MyString ms = new MyString(sc.nextLine()); + while (true) { + menu(); + System.out.print("Enter your choice: "); + int choice = sc.nextInt(); + sc.skip("\n"); + switch (choice) { + case 1: + System.out.print("Enter a string: "); + ms = new MyString(sc.nextLine()); + break; + case 2: + System.out.println("The string is: " + ms.valueOf()); + break; + case 3: + int count = ms.countWords(); + System.out.println("The string has " + count + " words."); + break; + case 4: + System.out.println("The given string reversed is: " + ms.reverse().valueOf()); + break; + case 5: + System.out.println("The given string is" + + (ms.isCaseInsensitivePalindrome() ? "" : "n't") + + " case-insensitively palindrome."); + break; + case 6: + System.out.println("Bye."); + return; + default: + System.out.println("Invalid choice, try again."); + } + } + } +} |