From 5f72840db4983d6ad962cd12fe9e2294d37627ed Mon Sep 17 00:00:00 2001 From: KhushiAgarwal0406 Date: Tue, 7 Oct 2025 17:46:36 +0530 Subject: [PATCH] Added Removing stars program --- .../thealgorithms/strings/ComplexNumber.java | 44 ++++++++++++++ .../thealgorithms/strings/Removing stars.java | 58 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/ComplexNumber.java create mode 100644 src/main/java/com/thealgorithms/strings/Removing stars.java diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumber.java b/src/main/java/com/thealgorithms/strings/ComplexNumber.java new file mode 100644 index 000000000000..42cae15ea998 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexNumber.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +public class ComplexMultiply { + /** + * Multiplies two complex numbers given as strings in "a+bi" format. + * Parses the strings, performs multiplication, and returns the product. + * + * num1 First complex number string + * num2 Second complex number string + * return the product as a string in "x+yi" format + */ + public static String multiply(String num1, String num2) { + int n1 = num1.indexOf('+'); + int a = Integer.parseInt(num1.substring(0, n1)); + int b = Integer.parseInt(num1.substring(n1 + 1, num1.length() - 1)); // exclude 'i' + + int n2 = num2.indexOf('+'); + int c = Integer.parseInt(num2.substring(0, n2)); + int d = Integer.parseInt(num2.substring(n2 + 1, num2.length() - 1)); // exclude 'i' + + int real = a * c - b * d; // real part of product + int imag = a * d + b * c; // imaginary part of product + + return real + "+" + imag + "i"; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Prompt user to enter first complex number + System.out.print("num1: "); + String num1 = scanner.nextLine(); + + // Prompt user to enter second complex number + System.out.print("num2: "); + String num2 = scanner.nextLine(); + + // Perform multiplication and display result + String result = multiply(num1, num2); + System.out.println("Output: " + result); + + scanner.close(); + } +} diff --git a/src/main/java/com/thealgorithms/strings/Removing stars.java b/src/main/java/com/thealgorithms/strings/Removing stars.java new file mode 100644 index 000000000000..052a02bff47a --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/Removing stars.java @@ -0,0 +1,58 @@ +import java.util.Scanner; + +public class RemoveStars { + + /** + * Removes stars from the input string by deleting the closest non-star character to the left of each star along with the star itself. + * + * The input string containing stars (*) + * The resulting string after all stars and their closest left characters are removed + */ + public static String removeStars(String s1) { + // Use StringBuilder for efficient string manipulation + StringBuilder sc = new StringBuilder(); + + // Loop through each character in the input string + for (int i = 0; i < s1.length(); i++) { + char ch = s1.charAt(i); + + if (ch == '*') { + // When star is found, remove last character from sc if it exists + if (sc.length() > 0) { + sc.deleteCharAt(sc.length() - 1); + } + } else { + // Append non-star characters to sc + sc.append(ch); + } + } + + // Convert StringBuilder to string and return + return sc.toString(); + } + + public static void main(String[] args) { + // Create a Scanner object to read user input from the console + Scanner scanner = new Scanner(System.in); + + // Prompt the user to enter a string containing stars + System.out.print("Input: "); + + // Read the entire line entered by the user + if (scanner.hasNextLine()) { + String input = scanner.nextLine(); + + // Call the removeStars method to process the string input + String output = removeStars(input); + + // Print the resulting string after removing stars and their left characters + System.out.println("Output: " + output); + } else { + // Inform user if no input was provided + System.out.println("No input provided."); + } + + // Close the scanner to free resources + scanner.close(); + } +}