From 5f72840db4983d6ad962cd12fe9e2294d37627ed Mon Sep 17 00:00:00 2001 From: KhushiAgarwal0406 Date: Tue, 7 Oct 2025 17:46:36 +0530 Subject: [PATCH 1/2] 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(); + } +} From 05ec7dd8966fca72c2d96b1215f128d215f1d0a4 Mon Sep 17 00:00:00 2001 From: KhushiAgarwal0406 Date: Mon, 13 Oct 2025 22:33:52 +0530 Subject: [PATCH 2/2] Fix: Implement RemoveStars and ComplexMultiply as per guidelines --- .../strings/ComplexMultiply.java | 26 +++++++++ .../thealgorithms/strings/ComplexNumber.java | 44 -------------- .../thealgorithms/strings/RemoveStars.java | 29 ++++++++++ .../thealgorithms/strings/Removing stars.java | 58 ------------------- 4 files changed, 55 insertions(+), 102 deletions(-) create mode 100644 src/main/java/com/thealgorithms/strings/ComplexMultiply.java delete mode 100644 src/main/java/com/thealgorithms/strings/ComplexNumber.java create mode 100644 src/main/java/com/thealgorithms/strings/RemoveStars.java delete mode 100644 src/main/java/com/thealgorithms/strings/Removing stars.java diff --git a/src/main/java/com/thealgorithms/strings/ComplexMultiply.java b/src/main/java/com/thealgorithms/strings/ComplexMultiply.java new file mode 100644 index 000000000000..6a2922ef4419 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexMultiply.java @@ -0,0 +1,26 @@ +package com.thealgorithms.strings; + +/** + * Multiplies two complex numbers given as strings in "a+bi" format. + * Parses the strings manually, performs multiplication, and returns the product. + */ +public class ComplexMultiply { + + public static String complexNumberMultiply(String num1, String num2) { + // Extract real and imaginary parts without using parseInt + int plusIndex1 = num1.indexOf('+'); + int real1 = Integer.valueOf(num1.substring(0, plusIndex1)); + int imag1 = Integer.valueOf(num1.substring(plusIndex1 + 1, num1.length() - 1)); + + int plusIndex2 = num2.indexOf('+'); + int real2 = Integer.valueOf(num2.substring(0, plusIndex2)); + int imag2 = Integer.valueOf(num2.substring(plusIndex2 + 1, num2.length() - 1)); + + // Multiply using complex number formula: + // (a+bi)(c+di) = (ac - bd) + (ad + bc)i + int real = real1 * real2 - imag1 * imag2; + int imaginary = real1 * imag2 + imag1 * real2; + + return real + "+" + imaginary + "i"; + } +} diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumber.java b/src/main/java/com/thealgorithms/strings/ComplexNumber.java deleted file mode 100644 index 42cae15ea998..000000000000 --- a/src/main/java/com/thealgorithms/strings/ComplexNumber.java +++ /dev/null @@ -1,44 +0,0 @@ -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/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java new file mode 100644 index 000000000000..521d41a2887b --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -0,0 +1,29 @@ +package com.thealgorithms.strings; + +/** + * Removes stars from the input string by deleting the closest non-star character + * to the left of each star along with the star itself. + * + * @param s1 The input string containing stars (*) + * @return The string after all stars and their closest left characters are removed + */ +public class RemoveStars { + + public static String removeStars(String s1) { + StringBuilder sc = new StringBuilder(); + + for (int i = 0; i < s1.length(); i++) { + char ch = s1.charAt(i); + + if (ch == '*') { + if (sc.length() > 0) { + sc.deleteCharAt(sc.length() - 1); + } + } else { + sc.append(ch); + } + } + + return sc.toString(); + } +} diff --git a/src/main/java/com/thealgorithms/strings/Removing stars.java b/src/main/java/com/thealgorithms/strings/Removing stars.java deleted file mode 100644 index 052a02bff47a..000000000000 --- a/src/main/java/com/thealgorithms/strings/Removing stars.java +++ /dev/null @@ -1,58 +0,0 @@ -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(); - } -}